package com.dragon.mmochat.model.battle;

import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_1_7S;
import com.dragon.mmochat.message.Mmochat_3_11S;
import com.dragon.mmochat.message.Mmochat_4_11S;
import com.dragon.mmochat.message.Mmochat_4_12S;
import com.dragon.mmochat.message.Mmochat_4_14S;
import com.dragon.mmochat.message.Mmochat_5_10S;
import com.dragon.mmochat.message.Mmochat_SendHonorAwardToOldServer;
import com.dragon.mmochat.model.MmochatAnnouncement;
import com.dragon.mmochat.model.MmochatDailyActivity;
import com.dragon.mmochat.model.MmochatExtraRandomAward;
import com.dragon.mmochat.model.MmochatSerial;
import com.dragon.mmochat.model.MmochatStrengthDrug;
import com.dragon.mmochat.model.MmochatTeam;
import com.dragon.mmochat.model.activity.MmochatSystemState;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfNormal;
import com.dragon.mmochat.model.attackMode.MmochatAttackModeOfRescueTeamer;
import com.dragon.mmochat.model.award.MmochatEquipAward;
import com.dragon.mmochat.model.award.MmochatPropAward;
import com.dragon.mmochat.model.enumType.MmochatActionType;
import com.dragon.mmochat.model.enumType.MmochatBattleState;
import com.dragon.mmochat.model.enumType.MmochatBattleType;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatBossLevelType;
import com.dragon.mmochat.model.enumType.MmochatDailyActivityType;
import com.dragon.mmochat.model.enumType.MmochatEquipQualityType;
import com.dragon.mmochat.model.enumType.MmochatEquipType;
import com.dragon.mmochat.model.enumType.MmochatMaterialType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatNpcState;
import com.dragon.mmochat.model.enumType.MmochatOtherSystemSettingType;
import com.dragon.mmochat.model.enumType.MmochatPropType;
import com.dragon.mmochat.model.enumType.MmochatRoleAttributeType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.enumType.MmochatRoleType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatSmeType;
import com.dragon.mmochat.model.enumType.MmochatTaskStateType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.enumType.MmochatTeamStateType;
import com.dragon.mmochat.model.fuBen.MmochatGangFuBen;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapGuider;
import com.dragon.mmochat.model.map.MmochatMapGuiderData;
import com.dragon.mmochat.model.object.MmochatEquip;
import com.dragon.mmochat.model.object.MmochatMaterial;
import com.dragon.mmochat.model.object.MmochatProp;
import com.dragon.mmochat.model.object.MmochatPropEffect;
import com.dragon.mmochat.model.object.MmochatSuperMachine;
import com.dragon.mmochat.model.role.MmochatActivityNpc;
import com.dragon.mmochat.model.role.MmochatMonster;
import com.dragon.mmochat.model.role.MmochatMonsterFactory;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatRole;
import com.dragon.mmochat.model.role.MmochatTempNpc;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatGangMixedPkGame;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatMultiServerWuDaoPkGame;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatNo1PkGame;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatPersonalPkGame;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatWuDaoPkGame;
import com.dragon.mmochat.model.task.MmochatKillMonster;
import com.dragon.mmochat.model.task.MmochatNpcBattle;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatEquipService;
import com.dragon.mmochat.service.MmochatFriendService;
import com.dragon.mmochat.service.MmochatGangService;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatMapGuiderService;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.dragon.mmochat.service.MmochatPetService;
import com.dragon.mmochat.service.MmochatPkMachineService;
import com.dragon.mmochat.service.MmochatPropService;
import com.dragon.mmochat.service.MmochatStatisticsService;
import com.dragon.mmochat.service.MmochatTaskService;
import com.dragon.mmochat.service.MmochatTeamService;
import com.dragon.mmochat.service.MmochatTowerService;
import com.dragon.mmochat.util.CreateTlvWin;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.message.widget.AddDataTLV;
import com.skymobi.message.widget.ClearDataTLV;
import com.skymobi.message.widget.CloseWinTLV;
import com.skymobi.message.widget.CtrlTLVStructureRequest;
import com.skymobi.message.widget.SendDataTLV;
import com.skymobi.message.widget.ShowWaitingMsgBoxTLV;
import com.skymobi.message.widget.WinType;

//一个战场
public class MmochatBattle implements Serializable {
	private static Logger log = LoggerFactory.getLogger(MmochatBattle.class);

	private static final long serialVersionUID = -7528839479415294632L;
	private static MmochatSerial serial = new MmochatSerial();
	private int battleId;
	private int turn = 1;
	private boolean canEscape = true; // 是否允许逃跑
	private MmochatBattleState state = MmochatBattleState.战斗未开始; // 战场状态
	private long curStateStartTime = System.currentTimeMillis(); // 当前战场状态的开始时间
	private MmochatBattleType type;
	private int actionPlayNum; // 每回合动画播放角色数
	private long lastSystemMsgShowTick = 0; // 最近一次系统消息发布的时间；系统消息发布后一定时间内，不允许玩家发消息
	private long battleStartTime = System.currentTimeMillis();// 本次战斗开始时间

	// 观战列表
	Map<Integer, MmochatPlayer> audiences = new ConcurrentHashMap<Integer, MmochatPlayer>();

	// 左队+右队成员
	Map<Byte, MmochatRole> roles = new ConcurrentHashMap<Byte, MmochatRole>();
	// 左队<pos->role>,pos:0-9; 玩家优先放入左队
	Map<Byte, MmochatRole> leftTeam = new ConcurrentHashMap<Byte, MmochatRole>();
	// 右队<pos->role>,pos:10-19
	Map<Byte, MmochatRole> rightTeam = new ConcurrentHashMap<Byte, MmochatRole>();

	// 当前回合的操作: pos->action
	Map<Byte, MmochatAction> actions = new ConcurrentHashMap<Byte, MmochatAction>();

	// 当前回合战斗动画播放结束的角色: roleId->roleId
	Map<Integer, Integer> aniOverMap = new ConcurrentHashMap<Integer, Integer>();

	// 回合操作前状态改变的角色:pos->MmochatRoundStateChange
	Map<Byte, MmochatRoundStateChange> roundNewStates = new ConcurrentHashMap<Byte, MmochatRoundStateChange>();

	// 当前回合的输出
	List<MmochatRoundFrame> frames = new ArrayList<MmochatRoundFrame>();

	// 回合操作后状态变化的角色:pos->
	Map<Byte, MmochatRoundStateChange> newRoles_changeState = new ConcurrentHashMap<Byte, MmochatRoundStateChange>(); // 状态改变
	Map<Byte, Integer> newRoles_delRole = new ConcurrentHashMap<Byte, Integer>(); // 删除角色
	Map<Byte, MmochatRoundStateAddRole> newRoles_addRole = new ConcurrentHashMap<Byte, MmochatRoundStateAddRole>(); // 增加角色

	// 赢的队
	Map<Byte, MmochatRole> winTeam = new ConcurrentHashMap<Byte, MmochatRole>();

	// 输的队
	Map<Byte, MmochatRole> loseTeam = new ConcurrentHashMap<Byte, MmochatRole>();

	// 被打死的怪,野外打怪中用于计算奖励
	List<MmochatMonster> killedMonster = new ArrayList<MmochatMonster>();

	// 任务战斗的任务ID
	int taskBattle_taskId = 0;
	int taskBattle_roleId; // 执行任务的角色id

	// 临时NPC战斗的NPC-ID:<0为NPC;否则表示无NPC
	int tmpNpcId = 0;

	public MmochatBattle getClone() {
		return (MmochatBattle) MmochatUtil.deepClone(this);
	}

	public MmochatBattle() {
		this.battleId = serial.addAndGetSerialId();
	}

	public int getBattleId() {
		return battleId;
	}

	public void setBattleId(int battleId) {
		this.battleId = battleId;
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	// 是否达到战斗最大回合数
	public boolean isTouchRoundMax() {
		switch (type) {
		case 个人争霸赛:
		case 天下第一争霸赛:
			return turn >= MmochatConstant.maxRoundInSingleMatch;
		case 武道会比赛:
		case 跨服武道会比赛: {
			// 如果比赛双方都只有一个人，那么也是30回合结束；否则40回合结束
			int leftTeamPlayerNum = 0, rightTeamPlayerNum = 0;
			for (MmochatRole role : leftTeam.values()) {
				if (role instanceof MmochatPlayer) {
					leftTeamPlayerNum++;
				}
			}
			for (MmochatRole role : rightTeam.values()) {
				if (role instanceof MmochatPlayer) {
					rightTeamPlayerNum++;
				}
			}
			if (leftTeamPlayerNum == 1 && rightTeamPlayerNum == 1) {
				return turn >= MmochatConstant.maxRoundInSingleMatch;
			} else {
				return turn >= MmochatConstant.maxRoundInSingleMatch + 10;
			}
		}
		case 击杀战神: {
			return turn >= MmochatConstant.maxRoundForGod;
		}
		default:
			return turn >= MmochatConstant.maxRoundPerBattle + 20;
		}

	}

	// 达到最大战斗回合的处理
	public void dealTouchRoundMax() {
		switch (type) {
		case 个人争霸赛:
		case 天下第一争霸赛:
		case 武道会比赛:
		case 跨服武道会比赛: {
			// 双方都按输处理
			winTeam = new ConcurrentHashMap<Byte, MmochatRole>();
			loseTeam = leftTeam;
			for (Entry<Byte, MmochatRole> entry : rightTeam.entrySet()) {
				Byte pos = entry.getKey();
				MmochatRole role = entry.getValue();
				loseTeam.put(pos, role);
			}
			handleAward();
			break;
		}
		default:
			break;
		}
	}

	public boolean isOver() {
		return state == MmochatBattleState.战斗结束;
	}

	public void setOver() {
		setState(MmochatBattleState.战斗结束);
	}

	// 发送奖励
	@SuppressWarnings("deprecation")
	public void handleAward() {
		// 玩家数量
		int playerNum = 0;
		for (MmochatRole role : roles.values()) {
			if (role instanceof MmochatPlayer) {
				playerNum++;
			}
		}

		// 将剩下未消失可复活的怪物也加到killedMonster中
		for (MmochatRole role : roles.values()) {
			addKilledMonster(role);
		}

		switch (type) {
		case 野外打怪: {
			// 怪种类-->怪数量
			Map<MmochatRoleType, Integer> monsterTypeList = new ConcurrentHashMap<MmochatRoleType, Integer>();
			int monterLevel = 1000;
			for (MmochatMonster m : killedMonster) {
				monterLevel = Math.min(m.getLevel(), monterLevel);
				Integer num = monsterTypeList.get(m.getRoleType());
				if (num == null) {
					monsterTypeList.put(m.getRoleType(), 1);
				} else {
					num++;
					monsterTypeList.put(m.getRoleType(), num);
				}
			}

			// PVE中玩家是leftTeam
			if (leftTeam == winTeam) {
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				// 增加好友度
				MmochatFriendService.addFriendValue(players.values(), 1);

				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							int expAward = MmochatBattleService
									.getMonsterAwardExp(killedMonster, role,
											playerNum);
							// 系统双倍
							if (MmochatSystemState.isSystemDoubleAward()
									|| me.hasPropEffect(MmochatPropType.系统双倍令
											.getEffectKey())) {
								expAward *= MmochatSystemState
										.getSystemawardvalue();
							}

							// 道具:时间双倍,次数双倍
							boolean doubleProp = false;
							if (me.hasPropEffect(MmochatPropType.天尊令
									.getEffectKey())) {
								expAward *= 2;
								doubleProp = true;
							} else {
								MmochatPropEffect effect = me
										.getPropEffects()
										.get(MmochatPropType.道尊令.getEffectKey());
								if (effect != null
										&& effect.getRestAmount() > 0) {
									expAward *= 2;
									doubleProp = true;
									effect.addAmount(-1);
									if (effect.getRestAmount() <= 0) {
										me.delPropEffect(MmochatPropType.道尊令
												.getEffectKey());
									}
								}
							}

							boolean uplevel = me.addExp(expAward);
							if (expAward > 0) {
								msg += "你获得了"
										+ MmochatUtil.wrapColor(expAward,
												Color.green) + "点经验!";
							}
							me.addNormalBattleZhanji(playerNum);
							msg += "你获得了" + playerNum + "点战绩!";
							boolean petUplevel = false;
							MmochatPet pet = null;
							Byte myPos = getPos(me);
							Byte petPos = null;
							if (myPos != null) {
								petPos = getPetPos(myPos);
							}
							if (petPos != null) {
								MmochatRole myPet = roles.get((byte) petPos);
								if (myPet != null
										&& myPet instanceof MmochatPet) {
									pet = (MmochatPet) myPet;
									if (pet.getCurPkBlood() > 0) {
										if (pet.getLevel() >= me.getLevel()
												+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
											msg += "你的"
													+ pet.getName()
													+ "高于您"
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
													+ "级,无法获得经验!";
										} else {
											expAward = MmochatBattleService
													.getMonsterAwardExp(
															killedMonster, pet,
															playerNum);

											// 系统双倍
											if (MmochatSystemState
													.isSystemDoubleAward()
													|| me
															.hasPropEffect(MmochatPropType.系统双倍令
																	.getEffectKey())) {
												expAward *= MmochatSystemState
														.getSystemawardvalue();
											}

											// 道具:时间双倍,次数双倍
											if (doubleProp) {
												expAward *= 2;
											}
											// 道具:宠物双倍
											if (me
													.hasPropEffect(MmochatPropType.驱兽令
															.getEffectKey())) {
												expAward *= 2;
											} else {
												MmochatPropEffect effect = me
														.getPropEffects()
														.get(
																MmochatPropType.驭兽令
																		.getEffectKey());
												if (effect != null
														&& effect
																.getRestAmount() > 0) {
													expAward *= 2;
													effect.addAmount(-1);
													if (effect.getRestAmount() <= 0) {
														me
																.delPropEffect(MmochatPropType.驭兽令
																		.getEffectKey());
													}
												}
											}

											petUplevel = pet.addExp(me,
													expAward);
											msg += "你的"
													+ pet.getName()
													+ "获得了"
													+ MmochatUtil.wrapColor(
															expAward,
															Color.green)
													+ "点经验!";
										}
									}
								}
							}

							if (uplevel) {
								msg += "\n"
										+ MmochatUtil
												.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
										+ MmochatUtil.wrapColor("恭喜!你升到"
												+ me.getLevel() + "级了!",
												Color.green);
							}
							if (petUplevel && pet != null) {
								msg += "\n"
										+ MmochatUtil
												.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
										+ MmochatUtil.wrapColor("恭喜!你的"
												+ pet.getName() + "升到"
												+ pet.getLevel() + "级了!",
												Color.green);
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								if (petUplevel && pet != null) {
									if (pet1 == pet) {
										continue;
									}
								}
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 掉落装备
							List<MmochatEquip> equips = MmochatBattleService
									.getMonsterAwardEquips(killedMonster);
							if (equips != null && equips.size() > 0) {
								msg += "\n"
										+ MmochatUtil
												.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
								msg += "你获得了";
								for (MmochatEquip e : equips) {
									boolean success = me.addObjectToPackage(e);
									if (success) {
										msg += "1件"
												+ MmochatUtil.wrapColor(e
														.getName(), e
														.getQuality()
														.getColor()) + "!";
									} else {
										msg += "您的包裹空间不足,无法获得" + e.getName();
									}
								}
							}

							// 掉落游戏币
							int smallMoney = MmochatBattleService
									.getMonsterAwardSmallMoney(killedMonster,
											playerNum);
							if (smallMoney > 0) {
								msg += "\n"
										+ MmochatUtil
												.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
								msg += "你获得了" + smallMoney + "文钱!";
								me.addAndUpdateSmall_money(smallMoney);
								MmochatStatisticsService.addSmallMoneyEarn(
										MmochatSmallMoneyEarnType.野外打怪,
										smallMoney);
							}

							// 奖励师德值
							if (me.getStudents().size() > 0) {
								int totalScore = 0;
								for (Integer studentId : me.getStudents()
										.keySet()) {
									MmochatPlayer student = players
											.get(studentId);
									if (student != null) {
										// 徒弟等级与怪等级相差10级以内才有师德奖励
										if (Math.abs(monterLevel
												- student.getLevel()) < 10) {
											// 带0-49级,1场2点
											// 带50-64级,1场3点
											// 带65-74级,1场4点
											// >=75级，0点
											int addScore = 0;
											if (student.getLevel() < 50) {
												addScore = 2;
											} else if (student.getLevel() < 65) {
												addScore = 3;
											} else if (student.getLevel() < 75) {
												addScore = 4;
											}
											totalScore += addScore;
										}
									}
								}
								if (totalScore > 0) {
									me.addTeacherScore(totalScore);
									msg += "你获得了" + totalScore + "点师德值。";
								}
							}

							// 掉落材料
							Map<MmochatMaterialType, Integer> materails = new ConcurrentHashMap<MmochatMaterialType, Integer>(
									MmochatBattleService
											.getMonsterAwardMaterials(killedMonster));
							// 更新任务
							for (MmochatTask task : me.getUnfinishedTasks()) {
								// 收集材料
								if (materails != null && materails.size() > 0) {
									for (MmochatMaterial m : task
											.getFinish_need_materials()) {
										if (m.getNum() > 0) {
											int curNum = me.getPackageObjNum(m
													.getMaterialType());
											if (curNum >= m.getNum()) {
												// 材料已足够
												continue;
											} else {
												// 材料不足
												Integer addNum = materails
														.get(m
																.getMaterialType());
												if (addNum != null) {
													if (addNum + curNum >= m
															.getNum()) {
														// 打够材料
														materails
																.remove(m
																		.getMaterialType());
														boolean success = me
																.addMaterial(
																		m
																				.getMaterialType(),
																		addNum);
														if (success) {
															msg += "\n"
																	+ MmochatUtil
																			.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
															msg += "你获得了"
																	+ addNum
																	+ "个"
																	+ m
																			.getName()
																	+ ",你已经收集够了"
																	+ MmochatUtil
																			.wrapColor(
																					task
																							.getTaskName(),
																					Color.green)
																	+ "任务要求的"
																	+ MmochatUtil
																			.wrapColor(
																					m
																							.getName(),
																					Color.yellow)
																	+ "数量!";

															// 检查此任务是否可交付，如果可交付则更新当前任务
															if (me
																	.canFinishTask(task
																			.getTaskId())) {
																Mmochat_4_11S pack = new Mmochat_4_11S();
																pack
																		.setActionType(1);
																pack.setMe(me);
																pack
																		.setTask(task);
																MmochatUtil
																		.sendCommonPack(
																				me,
																				pack);

																// 更新NPC任务标志
																MmochatNpc npc = MmochatNpcService
																		.getNpcById(task
																				.getFinish_npcId());
																if (npc != null
																		&& npc
																				.getMapId() == me
																				.getMapId()) {
																	Mmochat_4_14S npcPack = new Mmochat_4_14S();
																	npcPack
																			.setMe(me);
																	npcPack
																			.addNpc(npc);
																	MmochatUtil
																			.sendCommonPack(
																					me,
																					npcPack);
																}
															}
														} else {
															msg += "\n"
																	+ MmochatUtil
																			.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
															msg += "您的包裹空间不足,无法获得"
																	+ m
																			.getName();
														}
													}
												}
											}
										}
									}
								}

								// 杀怪数量
								for (MmochatKillMonster killCondition : task
										.getKillList()) {
									if (killCondition.isKillMonsterFull()) {
										continue;
									}
									MmochatRoleType monsterType = killCondition
											.getMonsterType();
									Integer num = monsterTypeList
											.get(monsterType);
									if (num != null) {
										// 增加打怪数量
										boolean full = killCondition
												.addKillMonsterNum(num);
										if (full) {
											// 刚好杀够怪物
											msg += "\n"
													+ MmochatUtil
															.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
											msg += "你已经杀够"
													+ MmochatUtil.wrapColor(
															task.getTaskName(),
															Color.green)
													+ "任务要求的"
													+ MmochatUtil
															.wrapColor(
																	monsterType
																			.toString(),
																	Color.yellow)
													+ "数量!";
											// 检查此任务是否可交付，如果可交付则更新当前任务
											// 如果交任务NPC在当前地图，则同时更新NPC的任务标志
											if (me.canFinishTask(task
													.getTaskId())) {
												Mmochat_4_11S pack = new Mmochat_4_11S();
												pack.setActionType(1);
												pack.setMe(me);
												pack.setTask(task);
												MmochatUtil.sendCommonPack(me,
														pack);

												// 更新NPC任务标志
												MmochatNpc npc = MmochatNpcService
														.getNpcById(task
																.getFinish_npcId());
												if (npc != null
														&& npc.getMapId() == me
																.getMapId()) {
													Mmochat_4_14S npcPack = new Mmochat_4_14S();
													npcPack.setMe(me);
													npcPack.addNpc(npc);
													MmochatUtil.sendCommonPack(
															me, npcPack);
												}
											}
										}
									}
								}
							}

							// 添加非任务所需材料(任务材料已经在上面添加了)
							if (materails != null && materails.size() > 0) {
								msg += "\n"
										+ MmochatUtil
												.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
								msg += "你获得了";
								for (Entry<MmochatMaterialType, Integer> set : materails
										.entrySet()) {
									MmochatMaterialType type = set.getKey();
									Integer num = set.getValue();
									if (num != null && type != null) {
										boolean success = me.addMaterial(type,
												num);
										if (success) {
											msg += num + "个" + type.toString()
													+ "!";
										} else {
											msg += "您的包裹空间不足,无法获得"
													+ type.toString();
										}
									}
								}
							}

							// 掉落强化丹
							if (me.hasPropEffect(MmochatPropType.练丹要术
									.getEffectKey())) {
								if (MmochatUtil
										.isInPercent(MmochatConstant.strengthDrugDropRate)) {
									// 掉落普通强化丹
									MmochatPropType propType = MmochatStrengthDrug
											.getStrengthDrugTypeByMapId(me
													.getMapId());
									if (propType != null) {
										MmochatProp newProp = new MmochatProp(
												propType);
										boolean ret = me
												.addObjectToPackage(newProp);
										if (ret) {
											msg += "1个" + propType.toString()
													+ "!";
										} else {
											msg += "您的包裹空间不足,无法获得"
													+ propType.toString();
										}
									} else {
										msg += "本地图无法掉落强化丹!";
									}
								}

								if (MmochatUtil
										.isInPercent(MmochatConstant.superStrengthDrugDropRate)) {
									// 掉落超级强化丹
									MmochatPropType[] propTypeList = new MmochatPropType[] {
											MmochatPropType.强化丹o铁血,
											MmochatPropType.强化丹o高力,
											MmochatPropType.强化丹o通灵,
											MmochatPropType.强化丹o幻影 };
									int index = MmochatUtil
											.getRandomValue(propTypeList.length);
									MmochatPropType propType = propTypeList[index];
									if (propType != null) {
										MmochatProp newProp = new MmochatProp(
												propType);
										boolean ret = me
												.addObjectToPackage(newProp);
										if (ret) {
											msg += "1个" + propType.toString()
													+ "!";
										} else {
											msg += "您的包裹空间不足,无法获得"
													+ propType.toString();
										}
									}
								}
							}

							if (msg != null) {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}

							// 孵化一周年纪念蛋
							if (me.getPackageObjNum(MmochatPropType.一周年纪念蛋) > 0) {

								// 是否是在西郊
								if (me.getMapId() == 23) {
									// 检查是否有足够的宠物位置， 不足则进行提示
									if (me.isPetFull()) {
										MmochatChatService
												.sendSystemMsgToPerson(
														me,
														"您携带的宠物数量已达上限，"
																+ "请先存放部分宠物到仓库，"
																+ "以免纪念蛋孵化失败!");
									} else {
										// 平均打10场孵化一次
										if (MmochatUtil.isInPercent(10)) {
											// 孵化成功
											// 扣除纪念蛋
											me.delPackageObj(
													MmochatPropType.一周年纪念蛋, 1);

											if (MmochatUtil.isInPercent(0.02)) {
												// 获得纪念宠
												MmochatPet newPet = MmochatPetService
														.createPetByType(MmochatRoleType.龙三太子);
												if (newPet != null) {
													me.addPet(newPet);
													try {
														MmochatDao
																.updateRolePets(me);
													} catch (Exception e) {
														e.printStackTrace();
													}
													Mmochat_5_10S pack = new Mmochat_5_10S();
													pack.addPet(newPet);
													MmochatUtil.sendCommonPack(
															me, pack);

													String sysInfo = "哇!"
															+ MmochatUtil
																	.wrapColor(
																			me
																					.getName(),
																			Color.green)
															+ "人品超群，在孵化纪念蛋时，意外得获得了一只"
															+ MmochatUtil
																	.wrapColor(
																			"龙三太子(纪念兽)",
																			Color.yellow)
															+ "，运气好了真是挡也挡不住呀!";
													sysInfo = MmochatUtil
															.wrapColor(sysInfo,
																	Color.red);
													MmochatChatService
															.sendSystemMsg(sysInfo);
													String myInfo = MmochatUtil
															.wrapColor(
																	"恭喜恭喜!您成功孵化了一只"
																			+ MmochatUtil
																					.wrapColor(
																							"龙三太子(纪念兽)",
																							Color.yellow)
																			+ "!",
																	Color.red);
													MmochatFriendService
															.sendMsgToPersonByFriendMessage(
																	me, myInfo);
													MmochatChatService
															.sendSystemMsgToPerson(
																	me, myInfo);
												}
											} else {
												// 道行、战绩、金钱奖励
												String myInfo = "您的纪念蛋孵化了，蛋里空空如也!您获得了";
												int daoAward = MmochatUtil
														.getRandomValue(500,
																1000);
												int zhanJi = me.getTrueLevel() * 3;
												int money = MmochatUtil
														.getRandomValue(3000,
																10000);
												// 刷道比赛
												MmochatPropService
														.dealDaoActivity(me,
																daoAward);
												int trueDao = MmochatBattleService
														.getRealDao(me,
																daoAward);

												if (trueDao > 0) {
													me.addDao(trueDao);
													myInfo += MmochatUtil
															.wrapColor(trueDao
																	+ "天道行(削减前"
																	+ daoAward
																	+ "天)，",
																	Color.green);
												}
												me.addZhanji(zhanJi);
												myInfo += MmochatUtil
														.wrapColor(zhanJi
																+ "点战绩，",
																Color.green);
												me.addSmall_money(money);
												myInfo += MmochatUtil
														.wrapColor(
																money + "文钱",
																Color.green)
														+ "。您可以继续去挑战纪念兽，来孵化更多的纪念蛋。";

												myInfo = MmochatUtil.wrapColor(
														myInfo, Color.yellow);
												MmochatFriendService
														.sendMsgToPersonByFriendMessage(
																me, myInfo);
												MmochatChatService
														.sendSystemMsgToPerson(
																me, myInfo);
											}
										}
									}
								}
							}

							// 孵化年兽蛋
							if (me.getPackageObjNum(MmochatPropType.年兽蛋) > 0) {
								// 是否是在西郊
								if (me.getMapId() == 23) {
									// 检查是否有足够的宠物位置， 不足则进行提示
									if (me.isPetFull()) {
										MmochatChatService
												.sendSystemMsgToPerson(
														me,
														"您携带的宠物数量已达上限，"
																+ "请先存放部分宠物到仓库，"
																+ "以免年兽蛋孵化失败!");
									} else {
										// 平均打7场孵化一次
										if (MmochatUtil.isInPercent(15)) {
											// 孵化成功
											// 扣除年兽蛋
											me.delPackageObj(
													MmochatPropType.年兽蛋, 1);

											if (MmochatUtil.isInPercent(0.02)) {
												// 获得变异宠
												MmochatPet newPet = MmochatPetService
														.createPetByType(MmochatRoleType.白娘子);
												if (newPet != null) {
													me.addPet(newPet);
													try {
														MmochatDao
																.updateRolePets(me);
													} catch (Exception e) {
														e.printStackTrace();
													}
													Mmochat_5_10S pack = new Mmochat_5_10S();
													pack.addPet(newPet);
													MmochatUtil.sendCommonPack(
															me, pack);

													String sysInfo = "哇!"
															+ MmochatUtil
																	.wrapColor(
																			me
																					.getName(),
																			Color.green)
															+ "蛇年行大运，在孵化年兽蛋时，意外得获得了一个"
															+ MmochatUtil
																	.wrapColor(
																			"白娘子(变异)",
																			Color.yellow)
															+ "，运气好了真是挡也挡不住呀!";
													sysInfo = MmochatUtil
															.wrapColor(sysInfo,
																	Color.red);
													MmochatChatService
															.sendSystemMsg(sysInfo);
													String myInfo = MmochatUtil
															.wrapColor(
																	"恭喜恭喜!您成功孵化了一只"
																			+ MmochatUtil
																					.wrapColor(
																							"白娘子(变异)",
																							Color.yellow)
																			+ "!",
																	Color.red);
													MmochatFriendService
															.sendMsgToPersonByFriendMessage(
																	me, myInfo);
													MmochatChatService
															.sendSystemMsgToPerson(
																	me, myInfo);
												}
											} else {
												// 道行、战绩、金钱奖励
												String myInfo = "您的年兽蛋孵化了，蛋里空空如也!您获得了";
												int daoAward = MmochatUtil
														.getRandomValue(365,
																730);
												int zhanJi = me.getTrueLevel() * 3;
												int money = MmochatUtil
														.getRandomValue(3000,
																10000);
												// 刷道比赛
												MmochatPropService
														.dealDaoActivity(me,
																daoAward);
												int trueDao = MmochatBattleService
														.getRealDao(me,
																daoAward);

												if (trueDao > 0) {
													me.addDao(trueDao);
													myInfo += MmochatUtil
															.wrapColor(trueDao
																	+ "天道行(削减前"
																	+ daoAward
																	+ "天)，",
																	Color.green);
												}
												me.addZhanji(zhanJi);
												myInfo += MmochatUtil
														.wrapColor(zhanJi
																+ "点战绩，",
																Color.green);
												me.addSmall_money(money);
												myInfo += MmochatUtil
														.wrapColor(
																money + "文钱",
																Color.green)
														+ "。您可以继续去驱赶年兽，来孵化更多的年兽蛋。";

												myInfo = MmochatUtil.wrapColor(
														myInfo, Color.yellow);
												MmochatFriendService
														.sendMsgToPersonByFriendMessage(
																me, myInfo);
												MmochatChatService
														.sendSystemMsgToPerson(
																me, myInfo);
											}
										}
									}
								}
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(uplevel);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// 将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}

			break;
		}
		case 地府打怪: {
			if (winTeam == leftTeam) {

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							// 判断玩家有没有鬼灵珠时间
							if (me.getPropEffects().get(
									MmochatPropType.鬼灵珠.getEffectKey()) != null) {
								int daoAward = 0;
								for (MmochatMonster m : killedMonster) {
									daoAward += m.getKillExp();
								}

								// 系统双倍
								if (MmochatSystemState.isSystemDoubleAward()
										|| me
												.hasPropEffect(MmochatPropType.系统双倍令
														.getEffectKey())) {
									daoAward *= MmochatSystemState
											.getSystemawardvalue();
								}

								// 道具:时间双倍
								if (me.hasPropEffect(MmochatPropType.天尊令
										.getEffectKey())) {
									daoAward *= 2;
								}

								daoAward = MmochatUtil.getTeamAward(daoAward,
										playerNum);

								// 刷道比赛
								MmochatPropService
										.dealDaoActivity(me, daoAward);
								// 不作等级削减了
								int trueDao = MmochatBattleService.getRealDao(
										me, daoAward);

								if (trueDao > 0) {
									me.addDao(trueDao);
									msg += "你获得了" + trueDao + "天道行(削减前"
											+ daoAward + "天)!";
								}
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);
								msg += "你获得了" + playerNum + "点战绩!";

								MmochatPet pet = me.getPetInBattle();
								if (pet != null) {
									if (pet.getCurPkBlood() > 0) {
										trueDao = MmochatBattleService
												.getRealDao(pet, daoAward);
										if (trueDao > 0) {
											pet.addDao(trueDao);
											msg += "你的" + pet.getName() + "获得了"
													+ trueDao + "点武学";
										}
									}
								}
								for (MmochatPet pet1 : me.getPets().values()) {
									pet1.setCurBlood(Math.max(1, pet1
											.getCurPkBlood()));
									pet1.setCurSprit(Math.max(0, pet1
											.getCurPkSprit()));
								}

								// 掉落装备
								List<MmochatEquip> equips = MmochatBattleService
										.getMonsterAwardEquips(killedMonster);
								if (equips != null && equips.size() > 0) {
									msg += "\n"
											+ MmochatUtil
													.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG);
									msg += "你获得了";
									for (MmochatEquip e : equips) {
										boolean success = me
												.addObjectToPackage(e);
										if (success) {
											msg += "1件"
													+ MmochatUtil.wrapColor(e
															.getName(), e
															.getQuality()
															.getColor()) + "!";
										} else {
											msg += "您的包裹空间不足,无法获得"
													+ e.getName();
										}
									}
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							} else {
								// 此玩家没有使用鬼灵珠
								msg = "您没有鬼灵珠(商城有售)，无法获得道行奖励!";
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);

								for (MmochatPet pet : me.getPets().values()) {
									pet.setCurBlood(Math.max(1, pet
											.getCurPkBlood()));
									pet.setCurSprit(Math.max(0, pet
											.getCurPkSprit()));
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;

		}
		case 友好切磋: {
			break;
		}
		case 强制杀戮: {
			if (loseTeam.size() == 0 || winTeam.size() == 0) {
				// 一方逃跑
				break;
			}
			String loseName = "", winName = "";
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					loseName += MmochatUtil
							.wrapColor(me.getName(), Color.green)
							+ "、";
				}
			}
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					winName += MmochatUtil
							.wrapColor(me.getName(), Color.yellow)
							+ "、";
				}
			}
			String lostMsg = MmochatUtil.wrapColor("您被" + winName
					+ "在野外击杀，您阵亡了!", Color.red);
			String winMsg = MmochatUtil.wrapColor("我方成功将" + loseName + "杀死!",
					Color.yellow);

			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					// 玩家输了
					// 将玩家转移到出生地，并扣除相应死亡惩罚
					MmochatPlayer me = (MmochatPlayer) role;
					me.setDeadInBattle(true);
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							me, lostMsg);
				}
			}

			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							me, winMsg);
				}
			}
			break;
		}
		case 跨服擂台比武: {
			float loserPower = 0, winnerPower = 0;
			int winNum = 0, loseNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					winNum++;
					winnerPower += 1.0 * MmochatBattleService.getRolePower(me
							.getLevel(), me.getLife_turn());
				}
			}
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					loseNum++;
					loserPower += 1.0 * MmochatBattleService.getRolePower(me
							.getLevel(), me.getLife_turn());
				}
			}
			if (loserPower == 0 || winnerPower == 0) {
				break;
			}
			int totalLoseZhanji = 0;
			int minTeamNum = Math.min(winNum, loseNum);
			if (loserPower > winnerPower) {
				float param = loserPower / winnerPower;
				totalLoseZhanji = (int) (30 + 30 * (param - 1)) * minTeamNum;
			} else {
				float param = winnerPower / loserPower;
				totalLoseZhanji = (int) (30 - 30 * (param - 1)) * minTeamNum;
			}
			totalLoseZhanji = Math.max(10, totalLoseZhanji);
			totalLoseZhanji = Math.min(100 * minTeamNum, totalLoseZhanji);
			int loseZhanjiPerRole = totalLoseZhanji / Math.max(loseNum, 1);
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					me.addPk_score(-1 * loseZhanjiPerRole);
					MmochatChatService.sendSystemMsgToPerson(me, "您输掉了"
							+ loseZhanjiPerRole + "点竞技点!当前竞技点："
							+ me.getPk_score() + "点!");
					if (me.getPk_score() < MmochatConstant.minPkScoreNeed) {
						MmochatChatService.sendSystemMsgToPerson(me, "您的竞技点不足"
								+ MmochatConstant.minPkScoreNeed
								+ "点，无法继续比武，请退出!在原区服内在线每分钟将恢复5点竞技点!");
					}
					// 保存数据库
					MmochatMain.serverListDaoService.updatePkScore(me
							.getRoleId() % 10000000, me.getRegServerInstId(),
							me.getPk_score());

				}
			}
			int winZhanjiPerRole = totalLoseZhanji / winNum;
			winZhanjiPerRole = Math.max(winZhanjiPerRole, 1);
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					me.addPk_score(winZhanjiPerRole);
					MmochatChatService.sendSystemMsgToPerson(me, "您赢得了"
							+ winZhanjiPerRole + "点竞技点!当前竞技点："
							+ me.getPk_score() + "点!");
					// 保存数据库
					MmochatMain.serverListDaoService.updatePkScore(me
							.getRoleId() % 10000000, me.getRegServerInstId(),
							me.getPk_score());
				}
			}

			// float loserPower = 0, winnerPower = 0;
			// int winNum = 0, loseNum = 0;
			// long totalPkScore = 0;
			// for (MmochatRole role : winTeam.values()) {
			// if (role instanceof MmochatPlayer) {
			// MmochatPlayer me = (MmochatPlayer) role;
			// winNum++;
			// winnerPower += me.getPk_score() * me.getPk_score();
			// }
			// }
			// for (MmochatRole role : loseTeam.values()) {
			// if (role instanceof MmochatPlayer) {
			// MmochatPlayer me = (MmochatPlayer) role;
			// loseNum++;
			// loserPower += me.getPk_score() * me.getPk_score();
			// totalPkScore += me.getPk_score();
			// }
			// }
			// loserPower = Math.max(1, loserPower);
			// winnerPower = Math.max(1, winnerPower);
			// totalPkScore = Math.max(1, totalPkScore);
			// int totalLoseZhanji = 0;
			// int minTeamNum = Math.min(winNum, loseNum);
			// if (loserPower > winnerPower) {
			// float param = loserPower / winnerPower;
			// totalLoseZhanji = (int) (30 + 15 * (param - 1)) * minTeamNum;
			// } else {
			// float param = winnerPower / loserPower;
			// totalLoseZhanji = (int) (30 - 5 * (param - 1)) * minTeamNum;
			// }
			// totalLoseZhanji = Math.max(1, totalLoseZhanji);
			// totalLoseZhanji = Math.min(90 * minTeamNum, totalLoseZhanji);
			//
			// for (MmochatRole role : loseTeam.values()) {
			// if (role instanceof MmochatPlayer) {
			// MmochatPlayer me = (MmochatPlayer) role;
			// int loseZhanjiPerRole = (int) (totalLoseZhanji
			// * me.getPk_score() / totalPkScore);
			// me.addPk_score(-1 * loseZhanjiPerRole);
			// MmochatChatService.sendSystemMsgToPerson(me, "您输掉了"
			// + loseZhanjiPerRole + "点竞技点!当前竞技点："
			// + me.getPk_score() + "点!");
			// if (me.getPk_score() < MmochatConstant.minPkScoreNeed) {
			// MmochatChatService.sendSystemMsgToPerson(me, "您的竞技点不足"
			// + MmochatConstant.minPkScoreNeed
			// + "点，无法继续比武，请退出!在原区服内在线每分钟将恢复5点竞技点!");
			// }
			// // 保存数据库
			// MmochatMain.serverListDaoService.updatePkScore(me
			// .getRoleId() % 10000000, me.getRegServerInstId(),
			// me.getPk_score());
			// }
			// }
			// int winZhanjiPerRole = totalLoseZhanji / winNum;
			// winZhanjiPerRole = Math.max(winZhanjiPerRole, 1);
			// for (MmochatRole role : winTeam.values()) {
			// if (role instanceof MmochatPlayer) {
			// MmochatPlayer me = (MmochatPlayer) role;
			// me.addPk_score(winZhanjiPerRole);
			// MmochatChatService.sendSystemMsgToPerson(me, "您赢得了"
			// + winZhanjiPerRole + "点竞技点!当前竞技点："
			// + me.getPk_score() + "点!");
			// // 保存数据库
			// MmochatMain.serverListDaoService.updatePkScore(me
			// .getRoleId() % 10000000, me.getRegServerInstId(),
			// me.getPk_score());
			// }
			// }
			break;
		}
		case 比武场比武: {
			float loserPower = 0, winnerPower = 0;
			int winNum = 0, loseNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					winNum++;
					winnerPower += 1.0 * MmochatBattleService.getRolePower(me
							.getLevel(), me.getLife_turn());
				}
			}
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					loseNum++;
					loserPower += 1.0 * MmochatBattleService.getRolePower(me
							.getLevel(), me.getLife_turn());
				}
			}
			if (loserPower == 0 || winnerPower == 0) {
				break;
			}
			int totalLoseZhanji = 0;
			int minTeamNum = Math.min(winNum, loseNum);
			if (loserPower > winnerPower) {
				float param = loserPower / winnerPower;
				totalLoseZhanji = (int) (200 + 200 * (param - 1)) * minTeamNum;
			} else {
				float param = winnerPower / loserPower;
				totalLoseZhanji = (int) (200 - 200 * (param - 1)) * minTeamNum;
			}
			totalLoseZhanji = Math.max(10, totalLoseZhanji);
			totalLoseZhanji = Math.min(2000 * minTeamNum, totalLoseZhanji);
			int loseZhanjiPerRole = totalLoseZhanji / Math.max(loseNum, 1);
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					me.addZhanji(-1 * loseZhanjiPerRole);
					MmochatChatService.sendSystemMsgToPerson(me, "您输掉了"
							+ loseZhanjiPerRole + "点战绩!");
					// 保存数据库
					MmochatBattleService.saveSqlAfterBattle(me);
				}
			}
			int winZhanjiPerRole = totalLoseZhanji / winNum;
			winZhanjiPerRole = Math.max(winZhanjiPerRole, 1);
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					me.addZhanji(winZhanjiPerRole);
					MmochatChatService.sendSystemMsgToPerson(me, "您赢得了"
							+ winZhanjiPerRole + "点战绩!");
					// 保存数据库
					MmochatBattleService.saveSqlAfterBattle(me);
				}
			}
			break;
		}
		case 天下第一争霸赛: {
			// 如果比赛已经结束,则不作处理
			boolean over = false;
			for (MmochatRole role : roles.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					if (MmochatNo1PkGame.isWuDaoPkOver(me)) {
						over = true;
					}
					break;
				}
			}
			if (over) {
				break;
			}

			// 输的一方每人扣除一半积分，至少扣除1点积分,如果积分<=0分则踢出武道会
			// 赢的一方每人获得1分
			int loserNum = 0;
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						MmochatPlayer me = (MmochatPlayer) role;
						me.delCommonActivityScoreToHalf();
						me.addAndUpdateSmall_money(500);
						MmochatStatisticsService.addSmallMoneyEarn(
								MmochatSmallMoneyEarnType.天下第一争霸赛, 500);
						loserNum++;

						if (me.getCommonActivityScore() <= 0) {
							// 积分为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}

							MmochatChatService
									.sendSystemMsgToPerson(me,
											"您输了，获得500文钱，扣除一半积分，当前积分为0，无法继续战斗!请离开天下第一争霸赛会场。");

						} else {
							MmochatChatService.sendSystemMsgToPerson(me,
									"您输了，获得500文钱，扣除一半积分，当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			int winTeamPlayerNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						winTeamPlayerNum++;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (winTeamPlayerNum > 0) {
				int winMoney = 1000 * loserNum / winTeamPlayerNum;
				for (MmochatRole role : winTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							me.addCommonActivityScore(1);
							me.addAndUpdateSmall_money(winMoney);
							MmochatStatisticsService
									.addSmallMoneyEarn(
											MmochatSmallMoneyEarnType.天下第一争霸赛,
											winMoney);
							MmochatChatService.sendSystemMsgToPerson(me,
									"您赢了，获得" + winMoney + "文钱,当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			break;

		}
		case 个人争霸赛: {
			// 如果比赛已经结束,则不作处理
			boolean over = false;
			for (MmochatRole role : roles.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					if (MmochatPersonalPkGame.isWuDaoPkOver(me)) {
						over = true;
					}
					break;
				}
			}
			if (over) {
				break;
			}

			// 输的一方每人扣除一半积分,如果积分<=0分则踢出武道会
			int loserNum = 0;
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						MmochatPlayer me = (MmochatPlayer) role;
						me.delCommonActivityScoreToHalf();
						me.addAndUpdateSmall_money(500);
						MmochatStatisticsService.addSmallMoneyEarn(
								MmochatSmallMoneyEarnType.个人争霸赛, 500);
						loserNum++;

						if (me.getCommonActivityScore() <= 0) {
							// 积分为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}

							MmochatChatService
									.sendSystemMsgToPerson(me,
											"您输了，获得500文钱，扣除一半积分，当前积分为0，无法继续战斗!请离开个人争霸赛会场。");

						} else {
							MmochatChatService.sendSystemMsgToPerson(me,
									"您输了，获得500文钱，扣除一半积分，当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			int winTeamPlayerNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					winTeamPlayerNum++;
				}
			}
			if (winTeamPlayerNum > 0) {
				int winMoney = 1000 * loserNum / winTeamPlayerNum;
				for (MmochatRole role : winTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							me.addCommonActivityScore(1);
							me.addAndUpdateSmall_money(winMoney);
							MmochatStatisticsService.addSmallMoneyEarn(
									MmochatSmallMoneyEarnType.个人争霸赛, winMoney);
							MmochatChatService.sendSystemMsgToPerson(me,
									"您赢了，获得" + winMoney + "文钱,当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			break;

		}
		case 跨服武道会比赛: {
			// 如果比赛已经结束,则不作处理
			boolean over = false;
			Integer teamMapId = null;
			for (MmochatRole role : roles.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					if (MmochatMultiServerWuDaoPkGame.isWuDaoPkOver(me)) {
						over = true;
						break;
					}
					teamMapId = me.getMapId();
				}
			}
			if (over) {
				break;
			}

			// 积分：初始每人90分，最高不超过90分
			// 战斗时间：N分钟
			// 战斗胜利，8点-9点期间，积分减少(N-10)分；9点-10点期间，积分减少(1.5*N-10)分
			// 战斗失败，8点-9点期间，积分减少(N+15)分；9点-10点期间，积分减少(2*N+30)分
			// 战斗回合超限，未决出胜负，则双方都按战败扣除积分
			// 逃跑方按战败处理
			int battleMin = (int) ((System.currentTimeMillis() - battleStartTime) / MmochatConstant.MS_PER_MINUTE);
			if (battleMin <= 0) {
				battleMin = 1;
			}
			Date now = new Date();
			int loseAddScore;
			int winAddScore;
			String loseScoreRuleInfo;
			String winScoreRuleInfo;
			if (now.getHours() == 20) {
				loseAddScore = -1 * (battleMin + 15);
				winAddScore = 10 - battleMin;
				loseScoreRuleInfo = "(战斗时间+15)";
				winScoreRuleInfo = "(战斗时间-10)";
			} else {
				loseAddScore = -1 * (2 * battleMin + 30);
				winAddScore = (int) (10 - 1.5 * battleMin);
				loseScoreRuleInfo = "(2倍战斗时间+30)";
				winScoreRuleInfo = "(1.5倍战斗时间-10)";
			}
			int loseTeamNum = 0;
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						loseTeamNum++;
						MmochatPlayer me = (MmochatPlayer) role;
						me.addCommonActivityScore(loseAddScore);

						// int curMySerScore = 0;
						// Map<Integer, Integer> serverScore =
						// MmochatMultiServerWuDaoPkGame.serverScores
						// .get(teamMapId);
						// if (serverScore != null) {
						// Integer curScore = serverScore.get(me
						// .getRegServerInstId());
						// if (curScore != null) {
						// curMySerScore = curScore;
						// }
						// }

						if (me.getCommonActivityScore() <= 0) {
							// 积分为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}

							MmochatChatService
									.sendSystemMsgToPerson(
											me,
											"您输了，获得"
													+ MmochatConstant.honorAwardForMultiWuDaoLosePerPk
													+ "点声望。本次战斗" + battleMin
													+ "分钟，扣除"
													+ (-1 * loseAddScore)
													+ "点体力" + loseScoreRuleInfo
													+ "，当前体力为0，无法继续战斗!请离开武道会场。");

						} else {
							MmochatChatService
									.sendSystemMsgToPerson(
											me,
											"您输了，获得"
													+ MmochatConstant.honorAwardForMultiWuDaoLosePerPk
													+ "点声望，本次战斗"
													+ battleMin
													+ "分钟，扣除"
													+ (-1 * loseAddScore)
													+ "点体力"
													+ loseScoreRuleInfo
													+ "，当前体力为"
													+ me
															.getCommonActivityScore()
													+ "点!");
						}
						Mmochat_SendHonorAwardToOldServer pack = new Mmochat_SendHonorAwardToOldServer(
								me.getRegServerInstId());
						pack.setRoleId(me.getRoleId() % 10000000);
						pack
								.setAddHonor(MmochatConstant.honorAwardForMultiWuDaoLosePerPk);
						MmochatUtil.sendMultiServerPack(pack);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			Integer winTeamServerInstId = null;
			int winTeamNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					winTeamServerInstId = me.getRegServerInstId();
					winTeamNum++;
				}
			}
			if (winTeamNum > 0) {
				// int curServerScore = 0;
				// if (winTeamServerInstId != null && teamMapId != null) {
				// Map<Integer, Integer> serverScore =
				// MmochatMultiServerWuDaoPkGame.serverScores
				// .get(teamMapId);
				// if (serverScore != null) {
				// synchronized (serverScore) {
				// Integer curScore = serverScore
				// .get(winTeamServerInstId);
				// if (curScore == null) {
				// curScore = 0;
				// }
				// curScore += winTeamNum * loseTeamNum;
				// serverScore.put(winTeamServerInstId, curScore);
				// curServerScore = curScore;
				// }
				// }
				// }

				for (MmochatRole role : winTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							int myScore = me.getCommonActivityScore();
							myScore += winAddScore;
							if (myScore > MmochatMultiServerWuDaoPkGame.maxScore) {
								myScore = MmochatMultiServerWuDaoPkGame.maxScore;
							}
							if (myScore < 0) {
								myScore = 0;
							}
							me.setCommonActivityScore(myScore);

							String scoreInfo;
							if (winAddScore > 0) {
								scoreInfo = "增加" + winAddScore + "点体力";
							} else {
								scoreInfo = "扣除" + (-1 * winAddScore) + "点体力"
										+ winScoreRuleInfo;
							}

							MmochatChatService
									.sendSystemMsgToPerson(
											me,
											"您赢了，获得"
													+ MmochatConstant.honorAwardForMultiWuDaoWinPerPk
													+ "点声望，本次战斗"
													+ battleMin
													+ "分钟，"
													+ scoreInfo
													+ "当前体力"
													+ me
															.getCommonActivityScore()
													+ "点。");
							if (me.getCommonActivityScore() <= 0) {
								// 积分为0
								// 离开队伍
								if (me.isInTeam()) {
									MmochatTeam team = MmochatTeamService.teams
											.get(me.getTeamId());
									team.leaveTeam(me.getRoleId());
								}

								MmochatChatService.sendSystemMsgToPerson(me,
										"当前体力为0，无法继续战斗!请离开武道会场!");
							}

							Mmochat_SendHonorAwardToOldServer pack = new Mmochat_SendHonorAwardToOldServer(
									me.getRegServerInstId());
							pack.setRoleId(me.getRoleId() % 10000000);
							pack
									.setAddHonor(MmochatConstant.honorAwardForMultiWuDaoWinPerPk);
							MmochatUtil.sendMultiServerPack(pack);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			break;
		}
		case 武道会比赛: {
			// 如果比赛已经结束,则不作处理
			boolean over = false;
			for (MmochatRole role : roles.values()) {
				if (role instanceof MmochatPlayer) {
					MmochatPlayer me = (MmochatPlayer) role;
					if (MmochatWuDaoPkGame.isWuDaoPkOver(me)) {
						over = true;
					}
					break;
				}
			}
			if (over) {
				break;
			}

			// 输的一方每人扣除一半积分,如果积分<=0分则踢出武道会
			int loserNum = 0;
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						MmochatPlayer me = (MmochatPlayer) role;
						me.delCommonActivityScoreToHalf();
						me.addAndUpdateSmall_money(500);
						MmochatStatisticsService.addSmallMoneyEarn(
								MmochatSmallMoneyEarnType.武道会比赛, 500);
						loserNum++;

						if (me.getCommonActivityScore() <= 0) {
							// 积分为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}

							MmochatChatService
									.sendSystemMsgToPerson(me,
											"您输了，获得500文钱，扣除一半积分，当前积分为0，无法继续战斗!请离开武道会场。");

						} else {
							MmochatChatService.sendSystemMsgToPerson(me,
									"您输了，获得500文钱，扣除一半积分，当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			int winTeamPlayerNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					winTeamPlayerNum++;
				}
			}
			if (winTeamPlayerNum > 0) {
				int winMoney = 1000 * loserNum / winTeamPlayerNum;
				for (MmochatRole role : winTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							me.addCommonActivityScore(1);
							me.addAndUpdateSmall_money(winMoney);
							MmochatStatisticsService.addSmallMoneyEarn(
									MmochatSmallMoneyEarnType.武道会比赛, winMoney);
							MmochatChatService.sendSystemMsgToPerson(me,
									"您赢了，获得" + winMoney + "文钱,当前积分为"
											+ me.getCommonActivityScore()
											+ "分。");
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			break;
		}
		case 帮派大乱斗: {
			// 如果比赛已经结束,则不作处理
			if (MmochatGangMixedPkGame.isGangMixedPkOver()) {
				break;
			}

			// 输方扣除体力＝500
			// 输方获得积分＝10
			// 每场战斗输方玩家得到金钱1000
			int loseTeamPlayerNum = 0;
			for (MmochatRole role : loseTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						MmochatPlayer me = (MmochatPlayer) role;
						me.delCommonActivityScore(500);
						me.addAndUpdateSmall_money(1000);
						MmochatStatisticsService.addSmallMoneyEarn(
								MmochatSmallMoneyEarnType.帮派大乱斗, 1000);
						MmochatGang gang = MmochatGangMixedPkGame.gangs.get(me
								.getGangId());
						if (gang != null) {
							MmochatGangMember member = gang.getMembers().get(
									me.getRoleId());
							if (member != null) {
								gang.addGangActivityScore(10);
								member.addBuildValue(5);
								gang.addBuildValue(5);
							}
						}

						loseTeamPlayerNum++;

						if (me.getCommonActivityScore() <= 0) {
							// 体力为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}
							if (gang != null) {
								MmochatChatService
										.sendSystemMsgToPerson(
												me,
												"您输了，获得1000文钱，本帮增加10分，当前积分"
														+ gang
																.getGangActivityScore()
														+ "分，您体力扣除500，当前体力值为0，无法继续战斗!请离开帮派大乱斗会场。");
							} else {
								MmochatChatService
										.sendSystemMsgToPerson(me,
												"您输了，获得1000文钱，您已经离开帮派，无法继续战斗!请离开帮派大乱斗会场。");

							}
						} else {
							if (gang != null) {
								MmochatChatService.sendSystemMsgToPerson(me,
										"您输了，获得1000文钱，本帮增加10分，当前积分"
												+ gang.getGangActivityScore()
												+ "分，您体力扣除500，当前体力值为"
												+ me.getCommonActivityScore()
												+ "。");
							} else {
								MmochatChatService
										.sendSystemMsgToPerson(me,
												"您输了，获得1000文钱，您已经离开帮派，无法继续战斗!请离开帮派大乱斗会场。");
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			int winTeamPlayerNum = 0;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					winTeamPlayerNum++;
				}
			}

			if (winTeamPlayerNum <= 0) {
				winTeamPlayerNum = 1;
			}
			// 赢方扣除体力＝输方人数*60/赢方人数
			// 赢方获得积分＝输方人数*60/赢方人数
			// 赢方得到金钱 = 本场所得积分 X 100；
			int delScore = loseTeamPlayerNum * 60 / winTeamPlayerNum;
			int gangScore = loseTeamPlayerNum * 60 / winTeamPlayerNum;
			for (MmochatRole role : winTeam.values()) {
				if (role instanceof MmochatPlayer) {
					try {
						MmochatPlayer me = (MmochatPlayer) role;
						me.delCommonActivityScore(delScore);
						me.addAndUpdateSmall_money(100 * gangScore);
						MmochatStatisticsService.addSmallMoneyEarn(
								MmochatSmallMoneyEarnType.帮派大乱斗,
								100 * gangScore);
						MmochatGang gang = MmochatGangMixedPkGame.gangs.get(me
								.getGangId());
						if (gang != null) {
							MmochatGangMember member = gang.getMembers().get(
									me.getRoleId());
							if (member != null) {
								gang.addGangActivityScore(gangScore);
								member.addBuildValue(gangScore / 2);
								gang.addBuildValue(gangScore / 2);
							}
						}

						if (me.getCommonActivityScore() <= 0) {
							// 体力为0
							// 离开队伍
							if (me.isInTeam()) {
								MmochatTeam team = MmochatTeamService.teams
										.get(me.getTeamId());
								team.leaveTeam(me.getRoleId());
							}

							MmochatChatService.sendSystemMsgToPerson(me,
									"您赢了，获得" + (100 * gangScore) + "文钱，本帮增加"
											+ gangScore + "分，当前积分"
											+ gang.getGangActivityScore()
											+ "分，您体力消耗" + delScore
											+ "，当前体力值为0，无法继续战斗!请离开帮派大乱斗会场。");

						} else {
							MmochatChatService
									.sendSystemMsgToPerson(me, "您赢了，获得"
											+ (100 * gangScore) + "文钱，本帮增加"
											+ gangScore + "分，当前积分"
											+ gang.getGangActivityScore()
											+ "分，您体力消耗" + delScore + "，当前体力值为"
											+ me.getCommonActivityScore() + "。");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

			break;
		}
		case 每日等级任务: {
			// 有任务的玩家是否都算完成任务
			if (winTeam == leftTeam) {
				for (MmochatRole role : winTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;
						MmochatTask myTask = me.getTaskById(taskBattle_taskId);
						if (myTask == null
								|| !myTask.canFinish(me)
								|| myTask.getTaskState() == MmochatTaskStateType.已完成) {
							// 任务不存在或未完成或已完成
							continue;
						}
						MmochatTaskService.dealFinishingTask(me, myTask);
					}
				}
			} else {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 除暴安良:
		case 斩妖除魔:
		case 修仙之路: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			} else {
				break;
			}
			if (boss == null) {
				return;
			}
			if (winTeam == leftTeam) {
				Integer taskLevel = null;

				// 去除临时NPC,并设置NPC为离开
				taskLevel = boss.getLevel();
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 是否有额外奖励
				boolean hasExtraAward = false;
				if (MmochatUtil
						.isInPercent(MmochatConstant.extraAwardOfDaoRate)) {
					hasExtraAward = true;
				}

				// 设置任务为已完成并给玩家和宠物发奖励
				Integer turn = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							MmochatTask theTask = me
									.getTaskById(taskBattle_taskId);
							if (theTask != null) {
								theTask.setTaskState(MmochatTaskStateType.已完成);
								turn = theTask.getTaskTurn();
								theTask.addTaskTurn();

								// 下发删除任务
								Mmochat_4_12S taskPack = new Mmochat_4_12S();
								taskPack.setTaskId(theTask.getTaskId());
								MmochatUtil.sendCommonPack(me, taskPack);

								int baseDao = 0;
								if (type == MmochatBattleType.除暴安良) {
									baseDao = 8;
								} else if (type == MmochatBattleType.斩妖除魔) {
									baseDao = 10;
								} else if (type == MmochatBattleType.修仙之路) {
									baseDao = 12;
								}
								int daoAward = killedMonster.size() * baseDao;

								// 系统双倍
								if (MmochatSystemState.isSystemDoubleAward()
										|| me
												.hasPropEffect(MmochatPropType.系统双倍令
														.getEffectKey())) {
									daoAward *= MmochatSystemState
											.getSystemawardvalue();
								}

								// 道具:时间双倍
								if (me.hasPropEffect(MmochatPropType.天尊令
										.getEffectKey())) {
									daoAward *= 2;
								}

								daoAward = MmochatUtil.getTeamAward(daoAward,
										playerNum);
								daoAward = (int) (daoAward * (0.6 + 0.1 * turn));
								if (taskLevel == null) {
									taskLevel = me.getLevel();
								}
								int trueDao = MmochatBattleService
										.getRealDaoOfLevel2(daoAward, me,
												taskLevel);
								// 刷道比赛
								int initDao = trueDao;
								MmochatPropService.dealDaoActivity(me, initDao);

								trueDao = MmochatBattleService.getRealDao(me,
										trueDao);

								if (trueDao > 0) {
									me.addDao(trueDao);
									msg += "你获得了" + trueDao + "天道行(削减前"
											+ initDao + "天)!";

									// 随机额外奖励
									if (hasExtraAward) {
										MmochatExtraRandomAward extra = new MmochatExtraRandomAward();
										extra.setBaseAward(trueDao);
										extra.setRoleId(me.getRoleId());
										List<MmochatExtraRandomAward> list = MmochatBattleService.battleExtraAward
												.get(me.getRoleId());
										if (list == null) {
											list = new ArrayList<MmochatExtraRandomAward>();
											MmochatBattleService.battleExtraAward
													.put(me.getRoleId(), list);
										}
										list.add(extra);
										msg += MmochatUtil
												.wrapColor(
														"您获得了额外抽奖机会,按#键收取吧!",
														Color.red);

										// 下发#键消息
										CreateTlvWin tlv = new CreateTlvWin();
										tlv.setWinType(WinType.List);
										tlv.setLeftName("打开");
										tlv.setTitle("选择一个宝箱打开");
										tlv.setWinHeight(228);
										tlv.setRightName("返回");
										tlv.addRightTLV(new CloseWinTLV());

										tlv.addItemEvent("道行奖励宝箱一");
										tlv.addItemEvent("道行奖励宝箱二");
										tlv.addItemEvent("道行奖励宝箱三");
										tlv.addItemEvent("道行奖励宝箱四");
										tlv.addItemEvent("道行奖励宝箱五");
										tlv.addItemEvent("道行奖励宝箱六");
										tlv.addItemEvent("道行奖励宝箱七");
										tlv.addItemEvent("道行奖励宝箱八");
										tlv.addItemEvent("道行奖励宝箱九");
										tlv.addItemEvent("道行奖励宝箱十");
										tlv.addLeftTLV(new ClearDataTLV());
										tlv.addLeftTLV(new AddDataTLV(
												(int) extra.getId()));
										tlv
												.addLeftTLV(new SendDataTLV(
														MmochatConstant
																.getClientModuleId(),
														MmochatMain
																.getModuleInstID(),
														MmochatConstant.MSGCODE_MMOCHAT_C_3_15,
														(int) me.getRoleId()));
										ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
										wait.setShowCancel(false);
										tlv.addLeftTLV(wait);

										CtrlTLVStructureRequest awardPack = new CtrlTLVStructureRequest(
												0, tlv);
										awardPack.setSrcId(MmochatMain
												.getModuleInstID());
										awardPack.setDstId(MmochatConstant
												.getClientModuleId());
										awardPack
												.setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_10_20);
										MmochatUtil.sendCommonPack(me,
												awardPack);
									}

								}
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);
								msg += "你获得了" + playerNum + "点战绩!";

								MmochatPet pet = me.getPetInBattle();
								if (pet != null) {
									if (pet.getCurPkBlood() > 0) {
										trueDao = MmochatBattleService
												.getRealDaoOfLevel2(daoAward,
														pet, taskLevel);
										trueDao = MmochatBattleService
												.getRealDao(pet, trueDao);
										if (trueDao > 0) {
											pet.addDao(trueDao);
											msg += "你的" + pet.getName() + "获得了"
													+ trueDao + "点武学";
										}
									}
								}
								for (MmochatPet pet1 : me.getPets().values()) {
									pet1.setCurBlood(Math.max(1, pet1
											.getCurPkBlood()));
									pet1.setCurSprit(Math.max(0, pet1
											.getCurPkSprit()));
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							} else {
								// 此玩家没有任务
								msg = "您没有领取" + type.toString()
										+ "任务或您的任务与队长任务不一致，无法获得任务奖励。";
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);

								for (MmochatPet pet : me.getPets().values()) {
									pet.setCurBlood(Math.max(1, pet
											.getCurPkBlood()));
									pet.setCurSprit(Math.max(0, pet
											.getCurPkSprit()));
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 高人指点: {
			MmochatNpc fixedNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (fixedNpc == null) {
				break;
			}
			if (winTeam == leftTeam) {
				Integer taskLevel = null;

				// 从NPC上移除此临时任务
				MmochatTask task = fixedNpc.getTasks()
						.remove(taskBattle_taskId);
				if (task == null) {
					break;
				}
				try {
					taskLevel = task.getFinish_need_battles().getBoss()
							.getLevel();
				} catch (Exception e1) {
					break;
				}

				int level = Math.max(0, taskLevel);
				level = Math.min(taskLevel, MmochatConstant.npcMaxLevel);
				Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
						.get(level);
				boolean uplevel = false;
				int expAward;
				if (attrMap != null) {
					expAward = (int) (attrMap
							.get(MmochatRoleAttributeType.杀死怪物经验) * 6 * killedMonster
							.size());
				} else {
					break;
				}

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 设置任务为已完成并给玩家和宠物发奖励
				Integer turn = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							MmochatTask theTask = me
									.getTaskById(taskBattle_taskId);
							if (theTask != null) {
								theTask.setTaskState(MmochatTaskStateType.已完成);
								turn = theTask.getTaskTurn();
								theTask.addTaskTurn();

								// 下发删除任务
								Mmochat_4_12S taskPack = new Mmochat_4_12S();
								taskPack.setTaskId(theTask.getTaskId());
								MmochatUtil.sendCommonPack(me, taskPack);

								// 经验奖励
								int myExpAward = expAward;
								// 系统双倍
								if (MmochatSystemState.isSystemDoubleAward()
										|| me
												.hasPropEffect(MmochatPropType.系统双倍令
														.getEffectKey())) {
									myExpAward *= MmochatSystemState
											.getSystemawardvalue();
								}

								int exp = (int) (myExpAward * (0.6 + 0.1 * turn));

								// 道具:时间双倍
								if (me.hasPropEffect(MmochatPropType.天尊令
										.getEffectKey())) {
									exp *= 2;
								}

								exp = MmochatUtil.getTeamAward(exp, playerNum);
								if (exp > 0) {
									int myExp = MmochatBattleService
											.getRealExp2(exp, me, level);
									uplevel = me.addExp(myExp);
									me.addNormalBattleZhanji(playerNum);
									msg = "你获得了"
											+ MmochatUtil.wrapColor(myExp,
													Color.green) + "点经验!";
									if (uplevel) {
										msg += "你升到" + me.getLevel() + "级了!";
									}
									msg += "你获得了" + playerNum + "点战绩!";

									boolean petUplevel = false;
									MmochatPet pet = me.getPetInBattle();
									if (pet != null && pet.getCurPkBlood() > 0) {
										if (pet.getLevel() >= me.getLevel()
												+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
											msg += "你的"
													+ pet.getName()
													+ "高于您"
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
													+ "级,无法获得经验!";
										} else {
											myExp = MmochatBattleService
													.getRealExp2(exp, pet,
															level);

											// 道具:宠物双倍
											if (me
													.hasPropEffect(MmochatPropType.驱兽令
															.getEffectKey())) {
												myExp *= 2;
											}

											petUplevel = pet.addExp(me, myExp);
											msg += "你的"
													+ pet.getName()
													+ "获得了"
													+ MmochatUtil.wrapColor(
															myExp, Color.green)
													+ "点经验!";
										}

									}

									if (uplevel) {
										msg += "\n"
												+ MmochatUtil
														.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
												+ MmochatUtil.wrapColor(
														"恭喜!你升到"
																+ me.getLevel()
																+ "级了!",
														Color.green);
									}
									if (petUplevel && pet != null) {
										msg += "\n"
												+ MmochatUtil
														.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
												+ MmochatUtil.wrapColor("恭喜!你的"
														+ pet.getName() + "升到"
														+ pet.getLevel()
														+ "级了!", Color.green);
									}

									for (MmochatPet pet1 : me.getPets()
											.values()) {
										if (petUplevel && pet != null) {
											if (pet1 == pet) {
												continue;
											}
										}
										pet1.setCurBlood(Math.max(1, pet1
												.getCurPkBlood()));
										pet1.setCurSprit(Math.max(0, pet1
												.getCurPkSprit()));
									}
								}

								// 更新NPC任务标志
								Mmochat_4_14S npcPack = new Mmochat_4_14S();
								npcPack.setMe(me);
								npcPack.addNpc(fixedNpc);
								MmochatUtil.sendCommonPack(me, npcPack);

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							} else {
								// 此玩家没有任务
								msg = "您没有领取" + type.toString()
										+ "任务或您的任务与队长任务不一致，无法获得任务奖励。";
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);

								for (MmochatPet pet : me.getPets().values()) {
									pet.setCurBlood(Math.max(1, pet
											.getCurPkBlood()));
									pet.setCurSprit(Math.max(0, pet
											.getCurPkSprit()));
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 真假情报: {
			if (winTeam == leftTeam) {
				Integer taskLevel = null;

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 设置任务为已完成并给玩家和宠物发奖励
				Integer turn = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							MmochatTask theTask = me
									.getTaskById(taskBattle_taskId);
							if (theTask != null
									&& theTask.getTaskState() == MmochatTaskStateType.已接) {
								theTask.setTaskState(MmochatTaskStateType.已完成);
								turn = theTask.getTaskTurn();
								theTask.addTaskTurn();

								// 下发删除任务
								Mmochat_4_12S taskPack = new Mmochat_4_12S();
								taskPack.setTaskId(theTask.getTaskId());
								MmochatUtil.sendCommonPack(me, taskPack);

								// TODO:经验奖励根据玩家等级设置
								taskLevel = me.getTrueLevel();// theTask.getTaskLevel();

								int level = Math.max(0, taskLevel);
								level = Math.min(taskLevel,
										MmochatConstant.npcMaxLevel);
								Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
										.get(level);
								boolean uplevel = false;
								int expAward;
								int roleBaseExp = 1;
								if (attrMap != null) {
									expAward = (int) (attrMap
											.get(MmochatRoleAttributeType.杀死怪物经验) * 10 * killedMonster
											.size());
									roleBaseExp = expAward;

									// 系统双倍
									if (MmochatSystemState
											.isSystemDoubleAward()
											|| me
													.hasPropEffect(MmochatPropType.系统双倍令
															.getEffectKey())) {
										expAward *= MmochatSystemState
												.getSystemawardvalue();
									}
								} else {
									continue;
								}

								int exp = (int) (expAward * (0.6 + 0.1 * turn));

								// 道具:时间双倍
								if (me.hasPropEffect(MmochatPropType.天尊令
										.getEffectKey())) {
									exp *= 2;
								}

								me.addNormalBattleZhanji(playerNum);
								exp = MmochatUtil.getTeamAward(exp, playerNum);
								if (exp > 0) {
									int myExp = MmochatBattleService
											.getRealExp2(exp, me, level);
									uplevel = me.addExp(myExp);
									msg = "你获得了"
											+ MmochatUtil.wrapColor(myExp,
													Color.green) + "点经验!";
									if (uplevel) {
										msg += "你升到" + me.getLevel() + "级了!";
									}

									boolean petUplevel = false;
									MmochatPet pet = me.getPetInBattle();
									if (pet != null && pet.getCurPkBlood() > 0) {
										if (pet.getLevel() >= me.getLevel()
												+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
											msg += "你的"
													+ pet.getName()
													+ "高于您"
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
													+ "级,无法获得经验!";
										} else {
											int petLevel = pet.getTrueLevel();
											attrMap = MmochatPetService.monsterBaseAttackData
													.get(petLevel);
											if (attrMap != null) {
												expAward = (int) (attrMap
														.get(MmochatRoleAttributeType.杀死怪物经验) * 10 * killedMonster
														.size());
												roleBaseExp = Math.max(1,
														roleBaseExp);

												myExp = (int) (1.0 * exp
														* expAward / roleBaseExp);

												// 道具:宠物双倍
												if (me
														.hasPropEffect(MmochatPropType.驱兽令
																.getEffectKey())) {
													myExp *= 2;
												}

												petUplevel = pet.addExp(me,
														myExp);
												msg += "你的"
														+ pet.getName()
														+ "获得了"
														+ MmochatUtil
																.wrapColor(
																		myExp,
																		Color.green)
														+ "点经验!";
											}
										}

									}

									if (uplevel) {
										msg += "\n"
												+ MmochatUtil
														.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
												+ MmochatUtil.wrapColor(
														"恭喜!你升到"
																+ me.getLevel()
																+ "级了!",
														Color.green);
									}
									if (petUplevel && pet != null) {
										msg += "\n"
												+ MmochatUtil
														.wrapIcon(MmochatConstant.BMP_CHAT_SYSTEMTAG)
												+ MmochatUtil.wrapColor("恭喜!你的"
														+ pet.getName() + "升到"
														+ pet.getLevel()
														+ "级了!", Color.green);
									}

									for (MmochatPet pet1 : me.getPets()
											.values()) {
										if (petUplevel && pet != null) {
											if (pet1 == pet) {
												continue;
											}
										}
										pet1.setCurBlood(Math.max(1, pet1
												.getCurPkBlood()));
										pet1.setCurSprit(Math.max(0, pet1
												.getCurPkSprit()));
									}
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							} else {
								// 此玩家没有任务
								msg = "您没有领取" + type.toString()
										+ "任务或您的任务与队长任务不一致，无法获得任务奖励。";
								me.setCurBlood(Math.max(1, me.getCurPkBlood()));
								me.setCurSprit(Math.max(0, me.getCurPkSprit()));

								me.addNormalBattleZhanji(playerNum);

								for (MmochatPet pet : me.getPets().values()) {
									pet.setCurBlood(Math.max(1, pet
											.getCurPkBlood()));
									pet.setCurSprit(Math.max(0, pet
											.getCurPkSprit()));
								}

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								if (!me.isDeadConnection()) {
									if (msg != "") {
										MmochatChatService
												.sendSystemMsgToPerson(me, msg);
									}

									// 更新头像图标信息
									Mmochat_1_7S pack = new Mmochat_1_7S();
									pack.setMe(me);
									MmochatUtil.sendCommonPack(me, pack);
								}
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 击杀BOSS: {
			// 会员BOSS
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				int bossLevel = boss.getLevel();
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}
							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// TODO:道行奖励
							int totalDao = MmochatBattleService
									.getRealDaoOfLevel2(365, me, boss
											.getLevel());
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, totalDao);

							int dao = MmochatBattleService.getRealDao(me,
									totalDao);
							me.addDao(dao);
							msg += "你获得了" + dao + "天道行(削减前" + totalDao + "天)!";

							if (pet != null) {
								totalDao = MmochatBattleService
										.getRealDaoOfLevel2(365, pet, boss
												.getLevel());
								dao = MmochatBattleService.getRealDao(pet,
										totalDao);
								pet.addDao(dao);
								msg += "你的" + pet.getName() + "获得了" + dao
										+ "点武学!";
							}

							// 2-5件蓝装,2件黄装
							int blueEquipNum = MmochatUtil.getRandomValue(2, 4);
							String equipInfo = null;
							for (int i = 0; i < blueEquipNum; i++) {
								MmochatEquip equip = MmochatEquipService
										.getRandEquip(bossLevel,
												bossLevel + 10, 0, 100, 0);
								if (equip != null) {
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
							}
							for (int i = 0; i < 2; i++) {
								MmochatEquip equip = MmochatEquipService
										.getRandEquip(bossLevel,
												bossLevel + 10, 0, 0, 100);
								if (equip != null) {
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
							}
							if (equipInfo != null) {
								msg += "你获得了" + equipInfo + "!";
							}

							if (MmochatUtil.isInPercent(20)) {
								MmochatProp prop = new MmochatProp(
										MmochatPropType.武魂石);
								boolean success = me.addObjectToPackage(prop);
								if (success) {
									msg += "您获得了一块"
											+ MmochatUtil.wrapColor(
													MmochatPropType.武魂石
															.toString(),
													Color.yellow) + "!";
								}
							}

							if (MmochatUtil.isInPercent(20)) {
								MmochatProp prop = new MmochatProp(
										MmochatPropType.武魂石灵气丹);
								boolean success = me.addObjectToPackage(prop);
								if (success) {
									msg += "您获得了一块"
											+ MmochatUtil.wrapColor(
													MmochatPropType.武魂石灵气丹
															.toString(),
													Color.yellow) + "!";
								}
							}

							// 如果是转生BOSS，有一定机率掉落N转进化石
							int lifeTurn = (boss.getLevel() - 120) / 10;
							if (lifeTurn > 0) {
								MmochatPropType propType = null;
								switch (lifeTurn) {
								case 1:
									propType = MmochatPropType.一转进化石;
									break;
								case 2:
									propType = MmochatPropType.二转进化石;
									break;
								case 3:
									propType = MmochatPropType.三转进化石;
									break;
								case 4:
									propType = MmochatPropType.四转进化石;
									break;
								case 5:
									propType = MmochatPropType.五转进化石;
									break;
								case 6:
									propType = MmochatPropType.六转进化石;
									break;
								case 7:
									propType = MmochatPropType.七转进化石;
									break;
								case 8:
									propType = MmochatPropType.八转进化石;
									break;
								case 9:
									propType = MmochatPropType.九转进化石;
									break;
								default:
									propType = MmochatPropType.一转进化石;
									break;
								}
								if (MmochatUtil.isInPercent(40)) {
									MmochatProp prop = new MmochatProp(propType);
									boolean success = me
											.addObjectToPackage(prop);
									if (success) {
										msg += "您获得了一块"
												+ MmochatUtil.wrapColor(
														propType.toString(),
														Color.yellow) + "!";
									}
								}
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 击杀战神: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				int bossLevel = boss.getLevel();
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);
				MmochatNpcService.npcs.remove(tmpNpcId);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				try {
					String nameList = "";
					for (MmochatRole role : leftTeam.values()) {
						if (role instanceof MmochatPlayer) {
							nameList += role.getName() + "、";
						}
					}
					MmochatChatService.sendSystemMsg("传闻"
							+ MmochatUtil.wrapColor(nameList, Color.green)
							+ "成功击杀了" + boss.getName() + "!");
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				// 发奖励
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							if (me.getTrueLevel() > boss.getLevel() + 10) {
								msg = "您的等级高于战神10级，无法获得奖励!";
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
								continue;
							}
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPkPet();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 2万天道，500倍经验，1个首饰，2个女娲石，1个小血石，2件金装，1个天尊令
							// 道行奖励
							int daoAward = 20000;
							int totalDao = MmochatBattleService
									.getRealDaoOfLevel2(daoAward, me, boss
											.getLevel());
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, totalDao);

							int dao = MmochatBattleService.getRealDao(me,
									totalDao);
							me.addDao(dao);
							msg += "你获得了" + dao + "天道行(削减前" + totalDao + "天)!";

							if (pet != null) {
								totalDao = MmochatBattleService
										.getRealDaoOfLevel2(daoAward, pet, boss
												.getLevel());
								dao = MmochatBattleService.getRealDao(pet,
										totalDao);
								pet.addDao(dao);
								msg += "你的" + pet.getName() + "获得了" + dao
										+ "点武学!";
							}

							// 经验奖励
							int level = Math.max(0, boss.getLevel());
							level = Math
									.min(level, MmochatConstant.npcMaxLevel);
							Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
									.get(level);
							boolean uplevel = false;
							if (attrMap != null) {
								int exp = (int) (attrMap
										.get(MmochatRoleAttributeType.杀死怪物经验) * 500);
								exp = MmochatUtil.getTeamAward(exp, playerNum);
								if (exp > 0) {
									int myExp = MmochatBattleService
											.getRealExp2(exp, me, level);
									uplevel = me.addExp(myExp);
									msg += "你获得了" + myExp + "点经验!";
									if (uplevel) {
										msg += "你升到" + me.getLevel() + "级了!";
									}
									if (pet != null) {
										if (pet.getLevel() >= me.getLevel()
												+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
											msg += "你的"
													+ pet.getName()
													+ "高于你"
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
													+ "级,无法获得经验!";
										} else {
											int petExp = MmochatBattleService
													.getRealExp2(exp, pet,
															level);
											boolean petUplevel = pet.addExp(me,
													petExp);
											msg += "你的" + pet.getName() + "获得了"
													+ petExp + "点经验!";
											if (petUplevel) {
												msg += "你的" + pet.getName()
														+ "升到" + pet.getLevel()
														+ "级了!";
											}
										}
									}
								}
							}

							// 20%机率获得一个随机武魂
							if (MmochatUtil.isInPercent(20)) {
								MmochatSuperMachine m = MmochatPkMachineService
										.getRandomPkMachine();
								boolean success = me.addObjectToPackage(m);
								if (success) {
									msg += "你获得了一个"
											+ MmochatUtil.wrapColor(m
													.getSuperMachineType()
													.toString(), Color.yellow)
											+ "!";
								} else {
									msg += "你的包裹已满，无法获得"
											+ MmochatUtil.wrapColor(m
													.getSuperMachineType()
													.toString(), Color.yellow)
											+ "!";
								}
							}

							// 1-3件黄装
							int blueEquipNum = MmochatUtil.getRandomValue(1, 2);
							String equipInfo = null;
							for (int i = 0; i < blueEquipNum; i++) {
								MmochatEquip equip = MmochatEquipService
										.getRandEquip(bossLevel,
												bossLevel + 10, 0, 0, 100);
								if (equip != null) {
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
							}

							// 1个首饰
							String jewelry = null;
							{
								MmochatEquipType equipType;
								int index = MmochatUtil.getRandomValue(1, 3);
								if (index == 1) {
									equipType = MmochatEquipType.手镯;
								} else if (index == 2) {
									equipType = MmochatEquipType.玉佩;
								} else {
									equipType = MmochatEquipType.挂坠;
								}
								{
									MmochatEquip equip = MmochatEquipService
											.createRandomEquip(true, equipType,
													0,
													MmochatEquipQualityType.金装);
									if (equip != null) {
										boolean success = me
												.addObjectToPackage(equip);
										if (success) {
											if (jewelry == null) {
												jewelry = MmochatUtil
														.wrapColor(
																equip.getName(),
																equip
																		.getQuality()
																		.getColor());
											} else {
												jewelry += ","
														+ MmochatUtil
																.wrapColor(
																		equip
																				.getName(),
																		equip
																				.getQuality()
																				.getColor());
											}
										} else {
											if (jewelry == null) {
												jewelry = equip.getName()
														+ ",但您的包裹已满,无法获得";
											} else {
												jewelry += ",包裹已满,无法获得"
														+ equip.getName();
											}
										}
									}
								}
							}
							if (jewelry != null) {
								msg += "你获得了" + jewelry + "。";
							}

							// 1个女娲石，1个小法池，0.5个天尊令, 0.5个驱兽令
							// 道具类型，最少数量，最多数量
							Object propAward[][] = {
									{ MmochatPropType.女娲石, 0, 1 },
									{ MmochatPropType.小法池, 0, 1 },
									{ MmochatPropType.天尊令, 0, 1 },
									{ MmochatPropType.驱兽令, 0, 1 },
									{ MmochatPropType.武魂石灵气丹, 0, 1 },
									{ MmochatPropType.武魂突破石, 0, 1 },
									{ MmochatPropType.武魂石, 0, 1 } };

							for (int i = 0; i < propAward.length; i++) {
								int num = MmochatUtil.getRandomValue(
										(Integer) propAward[i][1],
										(Integer) propAward[i][2]);
								for (int j = 0; j < num; j++) {
									MmochatProp prop = new MmochatProp(
											(MmochatPropType) propAward[i][0]);
									boolean success = me
											.addObjectToPackage(prop);
									if (success) {
										msg += "你获得了一个"
												+ MmochatUtil.wrapColor(prop
														.getType().toString(),
														Color.yellow) + "!";
									} else {
										msg += "你的包裹已满,无法获得"
												+ MmochatUtil.wrapColor(prop
														.getType().toString(),
														Color.yellow) + "!";
									}
								}
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(uplevel);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				if (boss.isTimeOut()) {
					MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
							.getMapId());
					if (theMap != null) {
						theMap.delNpc(boss.getLine(), tmpNpc);
					}
					boss.setState(MmochatNpcState.离开);
					MmochatNpcService.npcs.remove(tmpNpcId);
				}
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 击杀天干地支: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				int bossLevel = boss.getLevel();
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// TODO:道行奖励
							int totalDao = MmochatBattleService
									.getRealDaoOfLevel2(200, me, boss
											.getLevel());
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, totalDao);

							int dao = MmochatBattleService.getRealDao(me,
									totalDao);
							me.addDao(dao);
							msg += "你获得了" + dao + "天道行(削减前" + totalDao + "天)!";

							if (pet != null) {
								totalDao = MmochatBattleService
										.getRealDaoOfLevel2(200, pet, boss
												.getLevel());
								dao = MmochatBattleService.getRealDao(pet,
										totalDao);
								pet.addDao(dao);
								msg += "你的" + pet.getName() + "获得了" + dao
										+ "点武学!";
							}

							// 经验奖励
							int level = Math.max(0, boss.getLevel());
							level = Math
									.min(level, MmochatConstant.npcMaxLevel);
							Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
									.get(level);
							boolean uplevel = false;
							if (attrMap != null) {
								int exp = (int) (attrMap
										.get(MmochatRoleAttributeType.杀死怪物经验) * 60);
								exp = MmochatUtil.getTeamAward(exp, playerNum);
								if (exp > 0) {
									int myExp = MmochatBattleService
											.getRealExp2(exp, me, level);
									uplevel = me.addExp(myExp);
									msg += "你获得了" + myExp + "点经验!";
									if (uplevel) {
										msg += "你升到" + me.getLevel() + "级了!";
									}
									if (pet != null) {
										if (pet.getLevel() >= me.getLevel()
												+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
											msg += "你的"
													+ pet.getName()
													+ "高于你"
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
													+ "级,无法获得经验!";
										} else {
											int petExp = MmochatBattleService
													.getRealExp2(exp, pet,
															level);
											boolean petUplevel = pet.addExp(me,
													petExp);
											msg += "你的" + pet.getName() + "获得了"
													+ petExp + "点经验!";
											if (petUplevel) {
												msg += "你的" + pet.getName()
														+ "升到" + pet.getLevel()
														+ "级了!";
											}
										}
									}
								}
							}

							// 1-3件蓝装,1件黄装
							int blueEquipNum = MmochatUtil.getRandomValue(1, 3);
							String equipInfo = null;
							for (int i = 0; i < blueEquipNum; i++) {
								MmochatEquip equip = MmochatEquipService
										.getRandEquip(bossLevel,
												bossLevel + 10, 0, 100, 0);
								if (equip != null) {
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
							}

							{
								MmochatEquip equip = MmochatEquipService
										.getRandEquip(bossLevel,
												bossLevel + 10, 0, 0, 100);
								if (equip != null) {
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
								if (equipInfo != null) {
									msg += "你获得了" + equipInfo + "!";
								}
							}

							if (MmochatUtil.isInPercent(10)) {
								MmochatProp prop = new MmochatProp(
										MmochatPropType.武魂石);
								boolean success = me.addObjectToPackage(prop);
								if (success) {
									msg += "您获得了一块"
											+ MmochatUtil.wrapColor(
													MmochatPropType.武魂石
															.toString(),
													Color.yellow) + "!";
								}
							}

							if (MmochatUtil.isInPercent(10)) {
								MmochatProp prop = new MmochatProp(
										MmochatPropType.武魂石灵气丹);
								boolean success = me.addObjectToPackage(prop);
								if (success) {
									msg += "您获得了一块"
											+ MmochatUtil.wrapColor(
													MmochatPropType.武魂石灵气丹
															.toString(),
													Color.yellow) + "!";
								}
							}

							// 如果是转生十子，有一定机率掉落N转进化石
							int lifeTurn = (boss.getLevel() - 120) / 10;
							if (lifeTurn > 0) {
								MmochatPropType propType = null;
								switch (lifeTurn) {
								case 1:
									propType = MmochatPropType.一转进化石;
									break;
								case 2:
									propType = MmochatPropType.二转进化石;
									break;
								case 3:
									propType = MmochatPropType.三转进化石;
									break;
								case 4:
									propType = MmochatPropType.四转进化石;
									break;
								case 5:
									propType = MmochatPropType.五转进化石;
									break;
								case 6:
									propType = MmochatPropType.六转进化石;
									break;
								case 7:
									propType = MmochatPropType.七转进化石;
									break;
								case 8:
									propType = MmochatPropType.八转进化石;
									break;
								case 9:
									propType = MmochatPropType.九转进化石;
									break;
								default:
									propType = MmochatPropType.一转进化石;
									break;
								}
								if (MmochatUtil.isInPercent(20)) {
									MmochatProp prop = new MmochatProp(propType);
									boolean success = me
											.addObjectToPackage(prop);
									if (success) {
										msg += "您获得了一块"
												+ MmochatUtil.wrapColor(
														propType.toString(),
														Color.yellow) + "!";
									}
								}
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(uplevel);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 击杀鬼怪: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));
							for (MmochatPet pet : me.getPets().values()) {
								pet.setCurBlood(Math
										.max(1, pet.getCurPkBlood()));
								pet.setCurSprit(Math
										.max(0, pet.getCurPkSprit()));
							}

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							// 10000游戏币
							me.addAndUpdateSmall_money(10000);
							MmochatStatisticsService.addSmallMoneyEarn(
									MmochatSmallMoneyEarnType.击杀藏宝图鬼怪, 10000);
							msg = "你获得了" + MmochatUtil.getColorMoney(10000)
									+ "文钱";

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 击杀活动怪物: {
			MmochatActivityNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatActivityNpc) {
					boss = (MmochatActivityNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 设置boss等级
				int teamMaxLevel = 0;// 队伍最高等级
				Integer teamLeaderLevel = null;// 队长等级
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;
						teamMaxLevel = Math
								.max(teamMaxLevel, me.getTrueLevel());
						if (MmochatTeamService.getTeamState(me) == MmochatTeamStateType.队长) {
							teamLeaderLevel = me.getTrueLevel();
						}
					}
				}
				if (teamLeaderLevel == null) {
					teamLeaderLevel = teamMaxLevel;
				}
				MmochatMonsterFactory bossFactory = boss.getBattle().getBoss();
				if (bossFactory.getLevelType() == MmochatBossLevelType.队伍最高等级) {
					boss.setLevel(teamMaxLevel);
				} else if (bossFactory.getLevelType() == MmochatBossLevelType.队长等级) {
					boss.setLevel(teamLeaderLevel);
				}

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				for (MmochatRole role : leftTeam.values()) {
					String msg = "";
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 金钱奖励
							if (boss.getMoney() > 0) {
								me.addAndUpdateSmall_money(boss.getMoney());
								MmochatStatisticsService.addSmallMoneyEarn(
										MmochatSmallMoneyEarnType.击杀活动怪物, boss
												.getMoney());
								msg += "你获得了" + boss.getMoney() + "文钱!";
							}

							// 经验奖励,boss.getExp()为野外怪物经验奖励的倍数值
							boolean uplevel = false;
							if (boss.getExp() > 0) {
								int level = Math.max(0, boss.getLevel());
								level = Math.min(level,
										MmochatConstant.npcMaxLevel);
								Map<MmochatRoleAttributeType, Double> attrMap = MmochatPetService.monsterBaseAttackData
										.get(level);
								if (attrMap != null) {
									int exp = (int) (attrMap
											.get(MmochatRoleAttributeType.杀死怪物经验) * boss
											.getExp());
									exp = MmochatUtil.getTeamAward(exp,
											playerNum);
									if (exp > 0) {
										int myExp = MmochatBattleService
												.getRealExp2(exp, me, level);
										uplevel = me.addExp(myExp);
										msg += "你获得了" + myExp + "点经验!";
										if (uplevel) {
											msg += "你升到" + me.getLevel()
													+ "级了!";
										}
										if (pet != null) {
											if (pet.getLevel() >= me.getLevel()
													+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner) {
												msg += "你的"
														+ pet.getName()
														+ "高于你"
														+ MmochatConstant.zeroAwardOfPetLevelMoreThanOwner
														+ "级,无法获得经验!";
											} else {
												int petExp = MmochatBattleService
														.getRealExp2(exp, pet,
																level);
												boolean petUplevel = pet
														.addExp(me, petExp);
												msg += "你的" + pet.getName()
														+ "获得了" + petExp
														+ "点经验!";
												if (petUplevel) {
													msg += "你的" + pet.getName()
															+ "升到"
															+ pet.getLevel()
															+ "级了!";
												}
											}
										}
									}
								}
							}
							// 道行奖励
							if (boss.getDao() > 0) {
								int totalDao = MmochatBattleService
										.getRealDaoOfLevel2(boss.getDao(), me,
												boss.getLevel());
								// 刷道比赛
								MmochatPropService
										.dealDaoActivity(me, totalDao);

								int dao = MmochatBattleService.getRealDao(me,
										totalDao);
								me.addDao(dao);
								msg += "你获得了" + dao + "天道行(削减前" + totalDao
										+ "天)!";

								if (pet != null) {
									totalDao = MmochatBattleService
											.getRealDaoOfLevel2(boss.getDao(),
													pet, boss.getLevel());
									dao = MmochatBattleService.getRealDao(pet,
											totalDao);
									pet.addDao(dao);
									msg += "你的" + pet.getName() + "获得了" + dao
											+ "点武学!";
								}
							}
							// 战绩奖励
							if (boss.getZhangJi() > 0) {
								me.addZhanji(boss.getZhangJi());
								msg += "你获得了" + boss.getZhangJi() + "点战绩!";
							}

							// 道具奖励
							for (MmochatPropAward propAward : boss
									.getFinish_award_props()) {
								if (MmochatUtil.isInPercent(propAward
										.getOpportunity())) {
									MmochatProp prop = new MmochatProp(
											propAward.getPropType());
									prop.setCanDeal(propAward.isCanDeal());
									boolean success = me
											.addObjectToPackage(prop);
									if (success) {
										msg += "你获得了一个"
												+ MmochatUtil.wrapColor(prop
														.getType().toString(),
														Color.yellow) + "!";
									} else {
										msg += "你的包裹已满,无法获得"
												+ MmochatUtil.wrapColor(prop
														.getType().toString(),
														Color.yellow) + "!";
									}
								}
							}

							// 装备奖励
							String equipInfo = null;
							for (MmochatEquipAward equipAward : boss
									.getFinish_award_equips()) {
								if (MmochatUtil.isInPercent(equipAward
										.getOpportunity())) {
									MmochatEquip equip = MmochatEquipService
											.createRandomEquip(true, equipAward
													.getEquipType(), me
													.getLevel(), equipAward
													.getQuality());
									if (equip == null) {
										continue;
									}
									equip.setEquipModifyLevel(equipAward
											.getModify());
									boolean success = me
											.addObjectToPackage(equip);
									if (success) {
										if (equipInfo == null) {
											equipInfo = MmochatUtil.wrapColor(
													equip.getName(), equip
															.getQuality()
															.getColor());
										} else {
											equipInfo += ","
													+ MmochatUtil
															.wrapColor(
																	equip
																			.getName(),
																	equip
																			.getQuality()
																			.getColor());
										}
									} else {
										if (equipInfo == null) {
											equipInfo = equip.getName()
													+ ",但您的包裹已满,无法获得";
										} else {
											equipInfo += ",包裹已满,无法获得"
													+ equip.getName();
										}
									}
								}
							}
							if (equipInfo != null) {
								msg += "你获得了" + equipInfo + "。";
							}

							// 元宝奖励
							if (boss.getFinish_award_bigMoney() != null) {
								if (boss.getFinish_award_bigMoney()
										.getOpportunity() > 0) {
									if (MmochatUtil.isInPercent(boss
											.getFinish_award_bigMoney()
											.getOpportunity())) {
										int bigMoney = MmochatUtil
												.getRandomValue(
														boss
																.getFinish_award_bigMoney()
																.getMin(),
														boss
																.getFinish_award_bigMoney()
																.getMax());
										if (bigMoney > 0) {
											me.addBig_money(bigMoney);
											MmochatStatisticsService
													.addBigMoneyEarn(
															MmochatBigMoneyEarnType.击杀活动怪物,
															bigMoney);
											msg += "你获得了" + bigMoney + "个元宝!";
										}
									}
								}
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(uplevel);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				if (boss.isWillKillPlayer()) {
					for (MmochatRole role : leftTeam.values()) {
						if (role instanceof MmochatPlayer) {
							// 玩家输了
							MmochatPlayer me = (MmochatPlayer) role;
							me.setDeadInBattle(true);
						}
					}
				}
			}
			break;
		}
		case 挑战地图守护神: {
			if (MmochatGmService.isSystemExit()) {
				break;
			}
			// 必需胜利才能完成任务
			if (winTeam == leftTeam) {
				MmochatMap map = null;
				// 挑战成功，生成新的地图守护神数据
				List<MmochatTempNpc> npcList = new ArrayList<MmochatTempNpc>(); // NPC形象
				// 人物战斗数据
				List<MmochatMonsterFactory> rolePkDataList = new ArrayList<MmochatMonsterFactory>();
				// 宠物战斗数据
				List<MmochatMonsterFactory> petPkDataList = new ArrayList<MmochatMonsterFactory>();
				// 玩家roleId列表
				List<Integer> roleList = new ArrayList<Integer>();
				String nameList = "";

				int[] xDelta = new int[] { -70, -35, 0, 35, 70 };
				int[] yDelta = new int[] { 30, 15, 0, 15, 30 };
				int roleIndex = 0;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;
						me.getSme().clear();

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}
						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S(false);
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 扣除战绩
						me.addZhanji(-1
								* MmochatConstant.zhanjiCostPerMapGuiderPk);

						// 道行奖励
						String awardInfo = "";
						int totalDao = MmochatConstant.daoAwardOfWinMapGuiderPk;
						// 刷道比赛
						MmochatPropService.dealDaoActivity(me, totalDao);

						int dao = MmochatBattleService.getRealDao(me, totalDao);
						me.addDao(dao);
						awardInfo += "您获得了" + dao + "天道行(削减前" + totalDao
								+ "天)!";

						MmochatPet pet = me.getPetInBattle();
						if (pet != null) {
							totalDao = MmochatBattleService.getRealDaoOfLevel(
									totalDao, pet, me);
							int petDao = MmochatBattleService.getRealDao(pet,
									totalDao);
							pet.addDao(petDao);
							awardInfo += "您的" + pet.getName() + "获得了" + petDao
									+ "点武学!";
						}
						MmochatChatService.sendSystemMsgToPerson(me, "恭喜，挑战成功，"
								+ awardInfo + "您已成为新的守护神，其它队伍挑战失败时，您每场还能额外获得"
								+ MmochatConstant.daoAwardOfMapGuiderPerPk
								+ "天道行。额外道行奖励将在您被其它玩家取代守护神位置时进行发放。");

						roleList.add(me.getRoleId());
						nameList += me.getName() + "、";

						// 更新最近挑战时间
						MmochatDailyActivity acvitity = me.getDailyActivities()
								.get(MmochatDailyActivityType.挑战地图守护神);
						if (acvitity == null) {
							// 第一次挑战
							acvitity = new MmochatDailyActivity();
							acvitity.setType(MmochatDailyActivityType.挑战地图守护神);
							me.getDailyActivities().put(
									MmochatDailyActivityType.挑战地图守护神, acvitity);
						}
						acvitity.setLastTime(System.currentTimeMillis());

						// 保存道行、宠物、每日任务、战绩
						try {
							MmochatDao.saveMapGuiderData(me, dao);
						} catch (Exception e) {
							e.printStackTrace();
						}

						// 根据玩家生成NPC形象
						if (map == null) {
							map = MmochatMapService.maps.get(me.getMapId());
						}
						if (map != null) {
							MmochatMapGuider guiderInfo = map
									.getMapGuiderInfo();
							if (guiderInfo != null) {
								MmochatTempNpc boss = new MmochatTempNpc(true);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战守护神);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战规则及奖励);
								boss.getFunctionItems().add(
										MmochatNpcItemType.守护神分布);
								boss.setLevel(guiderInfo.getMyLevel());
								boss
										.setLifeTime(MmochatConstant.MS_PER_DAY * 3650);
								boss.setName(me.getName());
								boss.setBmpType(me.getBmpType());
								boss.setMapId(me.getMapId());
								boss
										.setX((short) (guiderInfo.getX() + xDelta[roleIndex]));
								boss
										.setY((short) (guiderInfo.getY() + yDelta[roleIndex]));
								boss.setFace(guiderInfo.getFace());

								npcList.add(boss);
							}
						}

						// 根据玩家生成人物战斗数据
						{
							// 人物技能目标数
							int skillTargetNum = 1;
							if (me.getLevel() < 27) {
								skillTargetNum = 1;
							} else if (me.getLevel() < 54) {
								skillTargetNum = 2;
							} else if (me.getLevel() < 80) {
								skillTargetNum = 3;
							} else if (me.getLevel() < 107) {
								skillTargetNum = 4;
							} else {
								skillTargetNum = 5;
							}
							MmochatMonsterFactory roleData = new MmochatMonsterFactory();
							rolePkDataList.add(roleData);

							roleData.setName(me.getName());
							roleData.setLevel(me.getLevel());
							roleData.setLife_turn(me.getLife_turn());
							roleData.setBmpType(me.getBmpType());
							roleData.setDao(me.getDao());
							roleData.setCanRescure(true);
							roleData.setFamily(me.getFamily());

							roleData.setTotalBlood(me.getTotalBlood() * 2);// 气血加倍
							roleData.setSpeed(me.getSpeed());
							roleData.setPhysicalAttack(me.getPhysicalAttack());
							roleData.setSpritAttack(me.getSpritAttack());
							roleData.setPhysicalDefend(me.getPhysicalDefend());
							roleData.setMagicDefend(me.getMagicDefend());
							roleData.setPhysicalAvoid(me.getPhysicalAvoid());
							roleData.setMagicAvoid(me.getMagicAvoid());
							roleData.setPhysicalDeadlyAttack(me
									.getPhysicalDeadlyAttack());
							roleData.setMagicDeadlyAttack(me
									.getMagicDeadlyAttack());
							roleData.setPhysicalDeadlyAttackDefend(me
									.getPhysicalDeadlyAttackDefend());
							roleData.setMagicDeadlyAttackDefend(me
									.getMagicDeadlyAttackDefend());
							roleData.setPhysicalDoubleAttack(me
									.getPhysicalDoubleAttack());
							roleData.setMagicDoubleAttack(me
									.getMagicDoubleAttack());
							roleData.setPhysicalDoubleAttackDefend(me
									.getPhysicalDoubleAttackDefend());
							roleData.setMagicDoubleAttackDefend(me
									.getMagicDoubleAttackDefend());
							roleData.setPhysicalDefendIgnore(me
									.getPhysicalDefendIgnore());
							roleData.setMagicDefendIgnore(me
									.getMagicDefendIgnore());
							roleData.setAntiSeal(me.getAntiSeal());
							roleData.setAntiFreeze(me.getAntiFreeze());
							roleData.setAntiConfuse(me.getAntiConfuse());
							roleData.setIgnoreAntiSeal(me.getIgnoreAntiSeal());
							roleData.setIgnoreAntiFreeze(me
									.getIgnoreAntiFreeze());
							roleData.setIgnoreAntiConfuse(me
									.getIgnoreAntiConfuse());
							roleData.setStrengthenSeal(me.getStrengthenSeal());
							roleData.setStrengthenFreeze(me
									.getStrengthenFreeze());
							roleData.setStrengthenConfuse(me
									.getStrengthenConfuse());
							roleData.setPhysicalDefendPercent(me
									.getPhysicalDefendPercent());
							roleData.setMagicDefendPercent(me
									.getMagicDefendPercent());
							roleData.setAntiGodSkill(me.getAntiGodSkill());
							roleData.setAntiHumanSkill(me.getAntiHumanSkill());
							roleData.setAntiDevilSkill(me.getAntiDevilSkill());
							roleData.setIgnoreAntiGodSkill(me
									.getIgnoreAntiGodSkill());
							roleData.setIgnoreAntiHumanSkill(me
									.getIgnoreAntiGodSkill());
							roleData.setIgnoreAntiDevilSkill(me
									.getIgnoreAntiDevilSkill());
							roleData.setSuperPhysicalAttack(me
									.getIgnoreAntiDevilSkill());
							roleData.setSuperSpritAttack(me
									.getSuperSpritAttack());
							roleData.setBossMaxSkillTargetNum(skillTargetNum);

							// 攻击模式,根据玩家加点类型和道行倍数进行设置
							// 加点模式
							int firstPointType = 0; // 主加点，0:力量；1:灵力；2:体质；3:敏捷
							int maxPointNum = me.getAttack_point();
							if (maxPointNum < me.getSprit_point()) {
								maxPointNum = me.getSprit_point();
								firstPointType = 1;
							}
							if (maxPointNum < me.getBlood_point()) {
								maxPointNum = me.getBlood_point();
								firstPointType = 2;
							}
							if (maxPointNum < me.getSpeed_point()) {
								maxPointNum = me.getSpeed_point();
								firstPointType = 3;
							}
							int secondPointType = 0; // 第二加点，0:力量；1:灵力；2:体质；3:敏捷
							int secondPointNum = -1;
							if (firstPointType != 0
									&& me.getAttack_point() > secondPointNum) {
								secondPointType = 0;
								secondPointNum = me.getAttack_point();
							}
							if (firstPointType != 1
									&& me.getSprit_point() > secondPointNum) {
								secondPointType = 1;
								secondPointNum = me.getSprit_point();
							}
							if (firstPointType != 2
									&& me.getBlood_point() > secondPointNum) {
								secondPointType = 2;
								secondPointNum = me.getBlood_point();
							}
							if (firstPointType != 3
									&& me.getSpeed_point() > secondPointNum) {
								secondPointType = 3;
								secondPointNum = me.getSpeed_point();
							}
							if (secondPointNum == me.getLevel()) {
								secondPointType = -1;
							}
							// 道行倍数决定障碍机率
							double daoParam = 1.0
									* me.getDao()
									/ Math.max(1, MmochatBattleService
											.getStandardDao(me.getLevel(), me
													.getLife_turn()));
							int limitOppotunity = 0;
							if (daoParam < 1) {
								limitOppotunity = 5;
							} else if (daoParam < 2) {
								limitOppotunity = 15;
							} else if (daoParam < 3) {
								limitOppotunity = 25;
							} else {
								limitOppotunity = 35;
							}

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							switch (firstPointType) {
							case 0:
								// 力量加点
								attackMode1
										.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
								attackMode1.setMagicAttackOppotunity((int) 0);
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							case 1:
								// 灵力加点
								attackMode1
										.setPhysicalAttackOpportunity((int) 0);
								attackMode1
										.setMagicAttackOppotunity((int) (100 - limitOppotunity));
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							case 2:
							case 3:
								// 敏捷加点、体质加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100 - limitOppotunity));
								} else if (secondPointType == 0) {
									// 第二主点在力量，则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								} else {
									// 第二主点不是力量和灵力，则增加障碍机率；其它为物攻操作
									limitOppotunity += 40;
									attackMode1
											.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							default:
								break;
							}
							roleData.setAttackMode_normal(attackMode1);

							// 救人模式
							MmochatAttackModeOfRescueTeamer rescueMode = new MmochatAttackModeOfRescueTeamer();
							rescueMode.setBloodPercent(30);
							switch (firstPointType) {
							case 0:
							case 1:
								// 打手，救人机率小
								rescueMode.setRescueOpportunity(10);
								break;
							case 2:
								// 体质加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(80);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(30);
								}
								break;
							case 3:
								// 敏捷加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(50);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(10);
								}
								break;
							default:
								break;
							}
							roleData.setAttackMode_rescueTeamer(rescueMode);
						}

						// 根据参战宠物生成宠物战斗数据
						MmochatPet pkPet = me.getPkPet();
						if (pkPet != null) {
							// 宠物技能目标数
							int skillTargetNum = 1;
							if (pkPet.getLevel() < 27) {
								skillTargetNum = 1;
							} else {
								skillTargetNum = 2;
							}
							MmochatMonsterFactory petData = new MmochatMonsterFactory();
							petPkDataList.add(petData);

							petData.setName(pkPet.getName());
							petData.setLevel(pkPet.getLevel());
							petData.setLife_turn(pkPet.getLife_turn());
							petData.setBmpType(pkPet.getBmpType());
							petData.setDao(pkPet.getDao());
							petData.setCanRescure(false);
							petData.setFamily(pkPet.getFamily());

							petData.setTotalBlood(pkPet.getTotalBlood());
							petData.setSpeed(pkPet.getSpeed());
							petData
									.setPhysicalAttack(pkPet
											.getPhysicalAttack());
							petData.setSpritAttack(pkPet.getSpritAttack());
							petData
									.setPhysicalDefend(pkPet
											.getPhysicalDefend());
							petData.setMagicDefend(pkPet.getMagicDefend());
							petData.setPhysicalAvoid(pkPet.getPhysicalAvoid());
							petData.setMagicAvoid(pkPet.getMagicAvoid());
							petData.setPhysicalDeadlyAttack(pkPet
									.getPhysicalDeadlyAttack());
							petData.setMagicDeadlyAttack(pkPet
									.getMagicDeadlyAttack());
							petData.setPhysicalDeadlyAttackDefend(pkPet
									.getPhysicalDeadlyAttackDefend());
							petData.setMagicDeadlyAttackDefend(pkPet
									.getMagicDeadlyAttackDefend());
							petData.setPhysicalDoubleAttack(pkPet
									.getPhysicalDoubleAttack());
							petData.setMagicDoubleAttack(pkPet
									.getMagicDoubleAttack());
							petData.setPhysicalDoubleAttackDefend(pkPet
									.getPhysicalDoubleAttackDefend());
							petData.setMagicDoubleAttackDefend(pkPet
									.getMagicDoubleAttackDefend());
							petData.setPhysicalDefendIgnore(pkPet
									.getPhysicalDefendIgnore());
							petData.setMagicDefendIgnore(pkPet
									.getMagicDefendIgnore());
							petData.setAntiSeal(pkPet.getAntiSeal());
							petData.setAntiFreeze(pkPet.getAntiFreeze());
							petData.setAntiConfuse(pkPet.getAntiConfuse());
							petData
									.setIgnoreAntiSeal(pkPet
											.getIgnoreAntiSeal());
							petData.setIgnoreAntiFreeze(pkPet
									.getIgnoreAntiFreeze());
							petData.setIgnoreAntiConfuse(pkPet
									.getIgnoreAntiConfuse());
							petData
									.setStrengthenSeal(pkPet
											.getStrengthenSeal());
							petData.setStrengthenFreeze(pkPet
									.getStrengthenFreeze());
							petData.setStrengthenConfuse(pkPet
									.getStrengthenConfuse());
							petData.setPhysicalDefendPercent(pkPet
									.getPhysicalDefendPercent());
							petData.setMagicDefendPercent(pkPet
									.getMagicDefendPercent());
							petData.setAntiGodSkill(pkPet.getAntiGodSkill());
							petData
									.setAntiHumanSkill(pkPet
											.getAntiHumanSkill());
							petData
									.setAntiDevilSkill(pkPet
											.getAntiDevilSkill());
							petData.setIgnoreAntiGodSkill(pkPet
									.getIgnoreAntiGodSkill());
							petData.setIgnoreAntiHumanSkill(pkPet
									.getIgnoreAntiGodSkill());
							petData.setIgnoreAntiDevilSkill(pkPet
									.getIgnoreAntiDevilSkill());
							petData.setSuperPhysicalAttack(pkPet
									.getIgnoreAntiDevilSkill());
							petData.setSuperSpritAttack(pkPet
									.getSuperSpritAttack());
							petData.setBossMaxSkillTargetNum(skillTargetNum);

							// 攻击模式,根据玩家加点类型和道行倍数进行设置
							// 加点模式
							int firstPointType = 0; // 主加点，0:力量；1:灵力；2:体质；3:敏捷
							int maxPointNum = pkPet.getAttack_point();
							if (maxPointNum < pkPet.getSprit_point()) {
								maxPointNum = pkPet.getSprit_point();
								firstPointType = 1;
							}
							if (maxPointNum < pkPet.getBlood_point()) {
								maxPointNum = pkPet.getBlood_point();
								firstPointType = 2;
							}
							if (maxPointNum < pkPet.getSpeed_point()) {
								maxPointNum = pkPet.getSpeed_point();
								firstPointType = 3;
							}
							int secondPointType = 0; // 第二加点，0:力量；1:灵力；2:体质；3:敏捷
							int secondPointNum = -1;
							if (firstPointType != 0
									&& pkPet.getAttack_point() > secondPointNum) {
								secondPointType = 0;
								secondPointNum = pkPet.getAttack_point();
							}
							if (firstPointType != 1
									&& pkPet.getSprit_point() > secondPointNum) {
								secondPointType = 1;
								secondPointNum = pkPet.getSprit_point();
							}
							if (firstPointType != 2
									&& pkPet.getBlood_point() > secondPointNum) {
								secondPointType = 2;
								secondPointNum = pkPet.getBlood_point();
							}
							if (firstPointType != 3
									&& pkPet.getSpeed_point() > secondPointNum) {
								secondPointType = 3;
								secondPointNum = pkPet.getSpeed_point();
							}
							if (secondPointNum == pkPet.getLevel()) {
								secondPointType = -1;
							}

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							switch (firstPointType) {
							case 0:
								// 力量加点
								attackMode1
										.setPhysicalAttackOpportunity((int) (100));
								attackMode1.setMagicAttackOppotunity((int) 0);
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 1:
								// 灵力加点
								attackMode1
										.setPhysicalAttackOpportunity((int) 0);
								attackMode1
										.setMagicAttackOppotunity((int) (100));
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 2:
								// 体质加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100));
								} else {
									// 否则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 3:
								// 敏捷加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100));
								} else {
									// 第二主点在力量，则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1.setLimitOppotunity((int) 0);
								break;
							default:
								break;
							}
							petData.setAttackMode_normal(attackMode1);

							// 救人模式:宠物以救人为主
							MmochatAttackModeOfRescueTeamer rescueMode = new MmochatAttackModeOfRescueTeamer();
							rescueMode.setBloodPercent(30);
							switch (firstPointType) {
							case 0:
							case 1:
								// 打手，救人机率小
								rescueMode.setRescueOpportunity(40);
								break;
							case 2:
								// 体质加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(90);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(60);
								}
								break;
							case 3:
								// 敏捷加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(90);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(70);
								}
								break;
							default:
								break;
							}
							petData.setAttackMode_rescueTeamer(rescueMode);
						}
						roleIndex++;
					}
				}

				MmochatTask newTask = new MmochatTask();
				newTask.setTaskId(0);
				MmochatNpcBattle battle = new MmochatNpcBattle();
				newTask.setFinish_need_battles(battle);
				newTask.setTaskType(MmochatTaskType.挑战地图守护神);
				MmochatMonsterFactory bigMonster = rolePkDataList.remove(0);
				List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();
				smallMonster.addAll(rolePkDataList);
				smallMonster.addAll(petPkDataList);
				battle.setBoss(bigMonster);
				battle.setSmallBoss(smallMonster);

				for (MmochatTempNpc npc : npcList) {
					npc.addTask(newTask);
				}
				if (map != null && npcList != null && npcList.size() == 5) {
					// 设置新的守护神
					MmochatMapGuiderService.setMapGuiderData(map, roleList,
							npcList);
					MmochatMapGuider guider = map.getMapGuiderInfo();
					if (guider != null) {
						// 系统公告
						String sysInfo = MmochatUtil.wrapColor(nameList,
								Color.green)
								+ "成为新一任"
								+ MmochatUtil.wrapColor(map.getMapName()
										+ "守护神", Color.yellow)
								+ "("
								+ map.getMapName()
								+ ":"
								+ guider.getX()
								+ ","
								+ guider.getY() + ")，";
						if (guider.getLifeTurnNeed() > 0) {
							sysInfo += guider.getLifeTurnNeed() + "转";
						}
						sysInfo += guider.getMinLevel() + "~"
								+ guider.getMaxLevel() + "级中谁人能敌?!";
						MmochatChatService.sendSystemMsg(sysInfo);
					}
				}

			} else {
				Integer mapId = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						MmochatPlayer me = (MmochatPlayer) role;

						if (mapId == null) {
							mapId = me.getMapId();
						}

						// 扣除战绩
						me.addZhanji(-1
								* MmochatConstant.zhanjiCostPerMapGuiderPk);

						// 道行奖励
						String awardInfo = "";
						int totalDao = MmochatConstant.daoAwardOfLoseMapGuiderPk;
						// 刷道比赛
						MmochatPropService.dealDaoActivity(me, totalDao);

						int dao = MmochatBattleService.getRealDao(me, totalDao);
						me.addDao(dao);
						awardInfo += "您获得了" + dao + "天道行(削减前" + totalDao
								+ "天)!";

						MmochatPet pet = me.getPetInBattle();
						if (pet != null) {
							totalDao = MmochatBattleService.getRealDaoOfLevel(
									totalDao, pet, me);
							int petDao = MmochatBattleService.getRealDao(pet,
									totalDao);
							pet.addDao(petDao);
							awardInfo += "您的" + pet.getName() + "获得了" + petDao
									+ "点武学!";
						}

						// 更新最近挑战时间
						MmochatDailyActivity acvitity = me.getDailyActivities()
								.get(MmochatDailyActivityType.挑战地图守护神);
						if (acvitity == null) {
							// 第一次挑战
							acvitity = new MmochatDailyActivity();
							acvitity.setType(MmochatDailyActivityType.挑战地图守护神);
							me.getDailyActivities().put(
									MmochatDailyActivityType.挑战地图守护神, acvitity);
						}
						acvitity.setLastTime(System.currentTimeMillis());

						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S();
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 保存道行、宠物、每日任务、战绩
						try {
							MmochatDao.saveMapGuiderData(me, dao);
						} catch (Exception e) {
							e.printStackTrace();
						}

						MmochatChatService.sendSystemMsgToPerson(me,
								"很遗憾，挑战失败，明天请继续努力!" + awardInfo);
					}
				}
				if (mapId != null) {
					// 守护神增加道行奖励
					MmochatMapGuiderData guider = MmochatMapGuiderService.guiderPositions
							.get(mapId);
					if (guider != null) {
						guider.addDao(MmochatConstant.daoAwardOfMapGuiderPerPk);
					}
				}
			}

			break;
		}
		case 挑战本派最强: {
			if (MmochatGmService.isSystemExit()) {
				break;
			}
			// 必需胜利才能完成任务
			if (winTeam == leftTeam) {
				MmochatMap map = null;
				// 挑战成功，生成新的门派最强数据
				List<MmochatTempNpc> npcList = new ArrayList<MmochatTempNpc>(); // NPC形象
				// 人物战斗数据
				List<MmochatMonsterFactory> rolePkDataList = new ArrayList<MmochatMonsterFactory>();
				// 宠物战斗数据
				List<MmochatMonsterFactory> petPkDataList = new ArrayList<MmochatMonsterFactory>();
				// 玩家roleId列表
				List<Integer> roleList = new ArrayList<Integer>();
				String nameList = "";

				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;
						me.getSme().clear();

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}
						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S(false);
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 扣除战绩
						me.addZhanji(-1
								* MmochatConstant.zhanjiCostPerMapGuiderPk);

						// 道行奖励
						String awardInfo = "";
						int totalDao = MmochatConstant.daoAwardOfWinMapGuiderPk;
						// 刷道比赛
						MmochatPropService.dealDaoActivity(me, totalDao);

						int dao = MmochatBattleService.getRealDao(me, totalDao);
						me.addDao(dao);
						awardInfo += "您获得了" + dao + "天道行(削减前" + totalDao
								+ "天)!";

						MmochatPet pet = me.getPetInBattle();
						if (pet != null) {
							totalDao = MmochatBattleService.getRealDaoOfLevel(
									totalDao, pet, me);
							int petDao = MmochatBattleService.getRealDao(pet,
									totalDao);
							pet.addDao(petDao);
							awardInfo += "您的" + pet.getName() + "获得了" + petDao
									+ "点武学!";
						}
						MmochatChatService.sendSystemMsgToPerson(me, "恭喜，挑战成功，"
								+ awardInfo + "您已成为本派最强，其它玩家挑战您失败时，您每场还能额外获得"
								+ MmochatConstant.daoAwardOfMapGuiderPerPk
								+ "天道行。额外道行奖励将在您被其它玩家取代本派最强位置时进行发放。");

						roleList.add(me.getRoleId());
						nameList += me.getName();

						// 更新最近挑战时间
						MmochatDailyActivity acvitity = me.getDailyActivities()
								.get(MmochatDailyActivityType.挑战本派最强);
						if (acvitity == null) {
							// 第一次挑战
							acvitity = new MmochatDailyActivity();
							acvitity.setType(MmochatDailyActivityType.挑战本派最强);
							me.getDailyActivities().put(
									MmochatDailyActivityType.挑战本派最强, acvitity);
						}
						acvitity.setLastTime(System.currentTimeMillis());

						// 保存道行、宠物、每日任务、战绩
						try {
							MmochatDao.saveMapGuiderData(me, dao);
						} catch (Exception e) {
							e.printStackTrace();
						}

						// 根据玩家生成NPC形象
						if (map == null) {
							map = MmochatMapService.maps.get(me.getMapId());
						}
						if (map != null) {
							MmochatMapGuider guiderInfo = map
									.getMapGuiderInfo();
							if (guiderInfo != null) {
								MmochatTempNpc boss = new MmochatTempNpc(true);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战本派最强);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战说明);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战经验锁妖塔);
								boss.getFunctionItems().add(
										MmochatNpcItemType.挑战道行锁妖塔);
								boss.getFunctionItems().add(
										MmochatNpcItemType.锁妖塔说明);
								boss.setLevel(guiderInfo.getMyLevel());
								boss
										.setLifeTime(MmochatConstant.MS_PER_DAY * 3650);
								boss.setName(map.getMapName() + "最强:"
										+ me.getName());
								boss.setBmpType(me.getBmpType());
								boss.setMapId(me.getMapId());
								boss.setX((short) guiderInfo.getX());
								boss.setY((short) guiderInfo.getY());
								boss.setFace(guiderInfo.getFace());

								npcList.add(boss);
							}
						}

						// 根据玩家生成人物战斗数据
						{
							// 人物技能目标数
							int skillTargetNum = 1;
							if (me.getLevel() < 27) {
								skillTargetNum = 1;
							} else if (me.getLevel() < 54) {
								skillTargetNum = 2;
							} else if (me.getLevel() < 80) {
								skillTargetNum = 3;
							} else if (me.getLevel() < 107) {
								skillTargetNum = 4;
							} else {
								skillTargetNum = 5;
							}
							MmochatMonsterFactory roleData = new MmochatMonsterFactory();
							rolePkDataList.add(roleData);

							roleData.setName(me.getName());
							roleData.setLevel(me.getLevel());
							roleData.setBmpType(me.getBmpType());
							roleData.setDao(me.getDao());
							roleData.setCanRescure(true);
							roleData.setFamily(me.getFamily());

							roleData
									.setTotalBlood((int) (me.getTotalBlood() * 1.5));// 气血加半
							roleData.setSpeed(me.getSpeed());
							roleData.setPhysicalAttack(me.getPhysicalAttack());
							roleData.setSpritAttack(me.getSpritAttack());
							roleData.setPhysicalDefend(me.getPhysicalDefend());
							roleData.setMagicDefend(me.getMagicDefend());
							roleData.setPhysicalAvoid(me.getPhysicalAvoid());
							roleData.setMagicAvoid(me.getMagicAvoid());
							roleData.setPhysicalDeadlyAttack(me
									.getPhysicalDeadlyAttack());
							roleData.setMagicDeadlyAttack(me
									.getMagicDeadlyAttack());
							roleData.setPhysicalDeadlyAttackDefend(me
									.getPhysicalDeadlyAttackDefend());
							roleData.setMagicDeadlyAttackDefend(me
									.getMagicDeadlyAttackDefend());
							roleData.setPhysicalDoubleAttack(me
									.getPhysicalDoubleAttack());
							roleData.setMagicDoubleAttack(me
									.getMagicDoubleAttack());
							roleData.setPhysicalDoubleAttackDefend(me
									.getPhysicalDoubleAttackDefend());
							roleData.setMagicDoubleAttackDefend(me
									.getMagicDoubleAttackDefend());
							roleData.setPhysicalDefendIgnore(me
									.getPhysicalDefendIgnore());
							roleData.setMagicDefendIgnore(me
									.getMagicDefendIgnore());
							roleData.setAntiSeal(me.getAntiSeal());
							roleData.setAntiFreeze(me.getAntiFreeze());
							roleData.setAntiConfuse(me.getAntiConfuse());
							roleData.setIgnoreAntiSeal(me.getIgnoreAntiSeal());
							roleData.setIgnoreAntiFreeze(me
									.getIgnoreAntiFreeze());
							roleData.setIgnoreAntiConfuse(me
									.getIgnoreAntiConfuse());
							roleData.setStrengthenSeal(me.getStrengthenSeal());
							roleData.setStrengthenFreeze(me
									.getStrengthenFreeze());
							roleData.setStrengthenConfuse(me
									.getStrengthenConfuse());
							roleData.setPhysicalDefendPercent(me
									.getPhysicalDefendPercent());
							roleData.setMagicDefendPercent(me
									.getMagicDefendPercent());
							roleData.setAntiGodSkill(me.getAntiGodSkill());
							roleData.setAntiHumanSkill(me.getAntiHumanSkill());
							roleData.setAntiDevilSkill(me.getAntiDevilSkill());
							roleData.setIgnoreAntiGodSkill(me
									.getIgnoreAntiGodSkill());
							roleData.setIgnoreAntiHumanSkill(me
									.getIgnoreAntiGodSkill());
							roleData.setIgnoreAntiDevilSkill(me
									.getIgnoreAntiDevilSkill());
							roleData.setSuperPhysicalAttack(me
									.getIgnoreAntiDevilSkill());
							roleData.setSuperSpritAttack(me
									.getSuperSpritAttack());
							roleData.setBossMaxSkillTargetNum(skillTargetNum);

							// 攻击模式,根据玩家加点类型和道行倍数进行设置
							// 加点模式
							int firstPointType = 0; // 主加点，0:力量；1:灵力；2:体质；3:敏捷
							int maxPointNum = me.getAttack_point();
							if (maxPointNum < me.getSprit_point()) {
								maxPointNum = me.getSprit_point();
								firstPointType = 1;
							}
							if (maxPointNum < me.getBlood_point()) {
								maxPointNum = me.getBlood_point();
								firstPointType = 2;
							}
							if (maxPointNum < me.getSpeed_point()) {
								maxPointNum = me.getSpeed_point();
								firstPointType = 3;
							}
							int secondPointType = 0; // 第二加点，0:力量；1:灵力；2:体质；3:敏捷
							int secondPointNum = -1;
							if (firstPointType != 0
									&& me.getAttack_point() > secondPointNum) {
								secondPointType = 0;
								secondPointNum = me.getAttack_point();
							}
							if (firstPointType != 1
									&& me.getSprit_point() > secondPointNum) {
								secondPointType = 1;
								secondPointNum = me.getSprit_point();
							}
							if (firstPointType != 2
									&& me.getBlood_point() > secondPointNum) {
								secondPointType = 2;
								secondPointNum = me.getBlood_point();
							}
							if (firstPointType != 3
									&& me.getSpeed_point() > secondPointNum) {
								secondPointType = 3;
								secondPointNum = me.getSpeed_point();
							}
							if (secondPointNum == me.getLevel()) {
								secondPointType = -1;
							}
							// 道行倍数决定障碍机率
							double daoParam = 1.0
									* me.getDao()
									/ Math.max(1, MmochatBattleService
											.getStandardDao(me.getLevel(), me
													.getLife_turn()));
							int limitOppotunity = 0;
							if (daoParam < 1) {
								limitOppotunity = 5;
							} else if (daoParam < 2) {
								limitOppotunity = 15;
							} else if (daoParam < 3) {
								limitOppotunity = 25;
							} else {
								limitOppotunity = 35;
							}

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							switch (firstPointType) {
							case 0:
								// 力量加点
								attackMode1
										.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
								attackMode1.setMagicAttackOppotunity((int) 0);
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							case 1:
								// 灵力加点
								attackMode1
										.setPhysicalAttackOpportunity((int) 0);
								attackMode1
										.setMagicAttackOppotunity((int) (100 - limitOppotunity));
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							case 2:
							case 3:
								// 敏捷加点、体质加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100 - limitOppotunity));
								} else if (secondPointType == 0) {
									// 第二主点在力量，则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								} else {
									// 第二主点不是力量和灵力，则增加障碍机率；其它为物攻操作
									limitOppotunity += 40;
									attackMode1
											.setPhysicalAttackOpportunity((int) (100 - limitOppotunity));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1
										.setLimitOppotunity((int) limitOppotunity);
								break;
							default:
								break;
							}
							roleData.setAttackMode_normal(attackMode1);

							// 救人模式
							MmochatAttackModeOfRescueTeamer rescueMode = new MmochatAttackModeOfRescueTeamer();
							rescueMode.setBloodPercent(30);
							switch (firstPointType) {
							case 0:
							case 1:
								// 打手，救人机率小
								rescueMode.setRescueOpportunity(10);
								break;
							case 2:
								// 体质加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(80);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(30);
								}
								break;
							case 3:
								// 敏捷加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(50);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(10);
								}
								break;
							default:
								break;
							}
							roleData.setAttackMode_rescueTeamer(rescueMode);
						}

						// 根据参战宠物生成宠物战斗数据
						MmochatPet pkPet = me.getPkPet();
						if (pkPet != null) {
							// 宠物技能目标数
							int skillTargetNum = 1;
							if (pkPet.getLevel() < 27) {
								skillTargetNum = 1;
							} else {
								skillTargetNum = 2;
							}
							MmochatMonsterFactory petData = new MmochatMonsterFactory();
							petPkDataList.add(petData);

							petData.setName(pkPet.getName());
							petData.setLevel(pkPet.getLevel());
							petData.setBmpType(pkPet.getBmpType());
							petData.setDao(pkPet.getDao());
							petData.setCanRescure(false);
							petData.setFamily(pkPet.getFamily());

							petData.setTotalBlood(pkPet.getTotalBlood());
							petData.setSpeed(pkPet.getSpeed());
							petData
									.setPhysicalAttack(pkPet
											.getPhysicalAttack());
							petData.setSpritAttack(pkPet.getSpritAttack());
							petData
									.setPhysicalDefend(pkPet
											.getPhysicalDefend());
							petData.setMagicDefend(pkPet.getMagicDefend());
							petData.setPhysicalAvoid(pkPet.getPhysicalAvoid());
							petData.setMagicAvoid(pkPet.getMagicAvoid());
							petData.setPhysicalDeadlyAttack(pkPet
									.getPhysicalDeadlyAttack());
							petData.setMagicDeadlyAttack(pkPet
									.getMagicDeadlyAttack());
							petData.setPhysicalDeadlyAttackDefend(pkPet
									.getPhysicalDeadlyAttackDefend());
							petData.setMagicDeadlyAttackDefend(pkPet
									.getMagicDeadlyAttackDefend());
							petData.setPhysicalDoubleAttack(pkPet
									.getPhysicalDoubleAttack());
							petData.setMagicDoubleAttack(pkPet
									.getMagicDoubleAttack());
							petData.setPhysicalDoubleAttackDefend(pkPet
									.getPhysicalDoubleAttackDefend());
							petData.setMagicDoubleAttackDefend(pkPet
									.getMagicDoubleAttackDefend());
							petData.setPhysicalDefendIgnore(pkPet
									.getPhysicalDefendIgnore());
							petData.setMagicDefendIgnore(pkPet
									.getMagicDefendIgnore());
							petData.setAntiSeal(pkPet.getAntiSeal());
							petData.setAntiFreeze(pkPet.getAntiFreeze());
							petData.setAntiConfuse(pkPet.getAntiConfuse());
							petData
									.setIgnoreAntiSeal(pkPet
											.getIgnoreAntiSeal());
							petData.setIgnoreAntiFreeze(pkPet
									.getIgnoreAntiFreeze());
							petData.setIgnoreAntiConfuse(pkPet
									.getIgnoreAntiConfuse());
							petData
									.setStrengthenSeal(pkPet
											.getStrengthenSeal());
							petData.setStrengthenFreeze(pkPet
									.getStrengthenFreeze());
							petData.setStrengthenConfuse(pkPet
									.getStrengthenConfuse());
							petData.setPhysicalDefendPercent(pkPet
									.getPhysicalDefendPercent());
							petData.setMagicDefendPercent(pkPet
									.getMagicDefendPercent());
							petData.setAntiGodSkill(pkPet.getAntiGodSkill());
							petData
									.setAntiHumanSkill(pkPet
											.getAntiHumanSkill());
							petData
									.setAntiDevilSkill(pkPet
											.getAntiDevilSkill());
							petData.setIgnoreAntiGodSkill(pkPet
									.getIgnoreAntiGodSkill());
							petData.setIgnoreAntiHumanSkill(pkPet
									.getIgnoreAntiGodSkill());
							petData.setIgnoreAntiDevilSkill(pkPet
									.getIgnoreAntiDevilSkill());
							petData.setSuperPhysicalAttack(pkPet
									.getIgnoreAntiDevilSkill());
							petData.setSuperSpritAttack(pkPet
									.getSuperSpritAttack());
							petData.setBossMaxSkillTargetNum(skillTargetNum);

							// 攻击模式,根据玩家加点类型和道行倍数进行设置
							// 加点模式
							int firstPointType = 0; // 主加点，0:力量；1:灵力；2:体质；3:敏捷
							int maxPointNum = pkPet.getAttack_point();
							if (maxPointNum < pkPet.getSprit_point()) {
								maxPointNum = pkPet.getSprit_point();
								firstPointType = 1;
							}
							if (maxPointNum < pkPet.getBlood_point()) {
								maxPointNum = pkPet.getBlood_point();
								firstPointType = 2;
							}
							if (maxPointNum < pkPet.getSpeed_point()) {
								maxPointNum = pkPet.getSpeed_point();
								firstPointType = 3;
							}
							int secondPointType = 0; // 第二加点，0:力量；1:灵力；2:体质；3:敏捷
							int secondPointNum = -1;
							if (firstPointType != 0
									&& pkPet.getAttack_point() > secondPointNum) {
								secondPointType = 0;
								secondPointNum = pkPet.getAttack_point();
							}
							if (firstPointType != 1
									&& pkPet.getSprit_point() > secondPointNum) {
								secondPointType = 1;
								secondPointNum = pkPet.getSprit_point();
							}
							if (firstPointType != 2
									&& pkPet.getBlood_point() > secondPointNum) {
								secondPointType = 2;
								secondPointNum = pkPet.getBlood_point();
							}
							if (firstPointType != 3
									&& pkPet.getSpeed_point() > secondPointNum) {
								secondPointType = 3;
								secondPointNum = pkPet.getSpeed_point();
							}
							if (secondPointNum == pkPet.getLevel()) {
								secondPointType = -1;
							}

							MmochatAttackModeOfNormal attackMode1 = new MmochatAttackModeOfNormal();
							switch (firstPointType) {
							case 0:
								// 力量加点
								attackMode1
										.setPhysicalAttackOpportunity((int) (100));
								attackMode1.setMagicAttackOppotunity((int) 0);
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 1:
								// 灵力加点
								attackMode1
										.setPhysicalAttackOpportunity((int) 0);
								attackMode1
										.setMagicAttackOppotunity((int) (100));
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 2:
								// 体质加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100));
								} else {
									// 否则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1.setLimitOppotunity((int) 0);
								break;
							case 3:
								// 敏捷加点
								if (secondPointType == 1) {
									// 第二主点在灵力，则法攻
									attackMode1
											.setPhysicalAttackOpportunity((int) 0);
									attackMode1
											.setMagicAttackOppotunity((int) (100));
								} else {
									// 第二主点在力量，则物攻
									attackMode1
											.setPhysicalAttackOpportunity((int) (100));
									attackMode1
											.setMagicAttackOppotunity((int) 0);
								}
								attackMode1.setLimitOppotunity((int) 0);
								break;
							default:
								break;
							}
							petData.setAttackMode_normal(attackMode1);

							// 救人模式:宠物以救人为主
							MmochatAttackModeOfRescueTeamer rescueMode = new MmochatAttackModeOfRescueTeamer();
							rescueMode.setBloodPercent(30);
							switch (firstPointType) {
							case 0:
							case 1:
								// 打手，救人机率小
								rescueMode.setRescueOpportunity(40);
								break;
							case 2:
								// 体质加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(90);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(60);
								}
								break;
							case 3:
								// 敏捷加点为主
								if (secondPointType != 0
										&& secondPointType != 1) {
									// 第二主点也不是伤害，则以救人为主
									rescueMode.setRescueOpportunity(90);
								} else {
									// 第二主点为伤害
									rescueMode.setRescueOpportunity(70);
								}
								break;
							default:
								break;
							}
							petData.setAttackMode_rescueTeamer(rescueMode);
						}
					}
				}

				MmochatTask newTask = new MmochatTask();
				newTask.setTaskId(0);
				MmochatNpcBattle battle = new MmochatNpcBattle();
				newTask.setFinish_need_battles(battle);
				newTask.setTaskType(MmochatTaskType.挑战本派最强);
				MmochatMonsterFactory bigMonster = rolePkDataList.remove(0);
				List<MmochatMonsterFactory> smallMonster = new ArrayList<MmochatMonsterFactory>();
				smallMonster.addAll(rolePkDataList);
				smallMonster.addAll(petPkDataList);
				battle.setBoss(bigMonster);
				battle.setSmallBoss(smallMonster);

				for (MmochatTempNpc npc : npcList) {
					npc.addTask(newTask);
				}
				if (map != null && npcList != null && npcList.size() == 1) {
					// 设置新的本派最强
					MmochatMapGuiderService.setFamilyGuiderData(map, roleList,
							npcList);
					MmochatMapGuider guider = map.getMapGuiderInfo();
					if (guider != null) {
						// 系统公告
						String sysInfo = MmochatUtil.wrapColor(nameList,
								Color.green)
								+ "勤学苦练，成为了新一代的"
								+ MmochatUtil.wrapColor(
										map.getMapName() + "最强", Color.yellow)
								+ "，我派弟子当以"
								+ MmochatUtil.wrapColor(nameList, Color.green)
								+ "为榜样!";
						MmochatChatService.sendSystemMsg(sysInfo);
					}
				}

			} else {
				Integer mapId = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						MmochatPlayer me = (MmochatPlayer) role;

						if (mapId == null) {
							mapId = me.getMapId();
						}

						// 扣除战绩
						me.addZhanji(-1
								* MmochatConstant.zhanjiCostPerMapGuiderPk);

						// 道行奖励
						String awardInfo = "";
						int totalDao = MmochatConstant.daoAwardOfLoseMapGuiderPk;
						// 刷道比赛
						MmochatPropService.dealDaoActivity(me, totalDao);

						int dao = MmochatBattleService.getRealDao(me, totalDao);
						me.addDao(dao);
						awardInfo += "您获得了" + dao + "天道行(削减前" + totalDao
								+ "天)!";

						MmochatPet pet = me.getPetInBattle();
						if (pet != null) {
							totalDao = MmochatBattleService.getRealDaoOfLevel(
									totalDao, pet, me);
							int petDao = MmochatBattleService.getRealDao(pet,
									totalDao);
							pet.addDao(petDao);
							awardInfo += "您的" + pet.getName() + "获得了" + petDao
									+ "点武学!";
						}

						// 更新最近挑战时间
						MmochatDailyActivity acvitity = me.getDailyActivities()
								.get(MmochatDailyActivityType.挑战本派最强);
						if (acvitity == null) {
							// 第一次挑战
							acvitity = new MmochatDailyActivity();
							acvitity.setType(MmochatDailyActivityType.挑战本派最强);
							me.getDailyActivities().put(
									MmochatDailyActivityType.挑战本派最强, acvitity);
						}
						acvitity.setLastTime(System.currentTimeMillis());

						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S();
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 保存道行、宠物、每日任务、战绩
						try {
							MmochatDao.saveMapGuiderData(me, dao);
						} catch (Exception e) {
							e.printStackTrace();
						}

						MmochatChatService.sendSystemMsgToPerson(me,
								"很遗憾，挑战失败，明天请继续努力!" + awardInfo);
					}
				}
				if (mapId != null) {
					// 守护神增加道行奖励
					MmochatMapGuiderData guider = MmochatMapGuiderService.familyGuiderPositions
							.get(mapId);
					if (guider != null) {
						guider.addDao(MmochatConstant.daoAwardOfMapGuiderPerPk);
					}
				}
			}

			break;

		}
		case 锁妖塔战斗: {
			if (winTeam == leftTeam) {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}

						MmochatTask task = me
								.getTaskByType(MmochatTaskType.挑战锁妖塔);
						if (task == null
								|| task.getTaskState() != MmochatTaskStateType.已接) {
							continue;
						}
						int lifeTurn = (task.getTaskLevel()
								+ MmochatConstant.startTowerLevelReduce - 120) / 10;
						if (lifeTurn < 0) {
							lifeTurn = 0;
						}
						int pureLevel = task.getTaskLevel()
								+ MmochatConstant.startTowerLevelReduce
								- lifeTurn * 10;
						String levelInfo = "";
						if (lifeTurn > 0) {
							levelInfo += lifeTurn + "转";
						}
						levelInfo += pureLevel + "级";
						MmochatPet pet = me.getPkPet();
						if (task.getTargetMapId() == MmochatConstant.maxTowerLevel
								- MmochatConstant.minTowerLevel
								+ MmochatConstant.mapId_锁妖塔20级) {
							synchronized (task) {
								if (task.getTaskState() == MmochatTaskStateType.已接) {
									task.setTaskState(MmochatTaskStateType.已完成);
								} else {
									continue;
								}
							}
							// 最后一层，任务完成，并发放奖励
							task.setTargetMapId(task.getTargetMapId() + 1);
							String info = "您完成了所有的锁妖塔挑战(共"
									+ MmochatTowerService
											.getTowerLevelCount(task)
									+ "层),获得了";
							if (task.getTaskTag() == MmochatConstant.taskTag_挑战经验通天塔) {
								int expAward = MmochatTowerService
										.getExpAwardOfTower(task);
								me.addExp(expAward);
								info += MmochatUtil.wrapColor(expAward + "点经验",
										Color.green)
										+ "，";
								if (pet != null) {
									int petExpAward = MmochatBattleService
											.getRealExp(expAward, me, pet);
									pet.addExp(me, petExpAward);
									info += "您的宠物"
											+ pet.getName()
											+ "获得了"
											+ MmochatUtil.wrapColor(petExpAward
													+ "点经验", Color.green) + "，";
								}
							} else {
								int daoAward = MmochatTowerService
										.getDaoAwardOfTower(task);
								// 刷道比赛
								MmochatPropService
										.dealDaoActivity(me, daoAward);

								int trueDao = MmochatBattleService.getRealDao(
										me, daoAward);
								me.addDao(trueDao);
								info += MmochatUtil.wrapColor(trueDao
										+ "天道行(削减前" + daoAward + "天)",
										Color.green)
										+ "，";
								if (pet != null) {
									int petDao = MmochatBattleService
											.getRealDaoOfLevel(daoAward, me,
													pet);
									int truePetDao = MmochatBattleService
											.getRealDao(pet, petDao);
									pet.addDao(truePetDao);
									info += "您的宠物"
											+ pet.getName()
											+ "获得了"
											+ MmochatUtil.wrapColor(truePetDao
													+ "点武学", Color.green) + "，";
								}
							}
							info += "今天的挑战已完成!";
							MmochatFriendService
									.sendSystemMsgToPersonByFriendMessage(me,
											info);
							MmochatChatService.sendSystemMsgToPerson(me, info);
							String sysInfo = MmochatUtil.wrapColor(
									me.getName(), Color.green)
									+ "完成了所有锁妖塔的挑战，真是旷世奇才呀!";
							MmochatChatService.sendSystemMsg(sysInfo);
							// 称号判断与处理，NPC是否替换
							boolean ret = MmochatTowerService.dealTowerBest(me,
									task);
							if (ret) {
								String bestInfo = MmochatUtil.wrapColor(me
										.getName(), Color.green)
										+ "完成了"
										+ MmochatUtil
												.wrapColor(
														MmochatTowerService
																.getTowerLevelCount(task)
																+ "层",
														Color.yellow)
										+ "的锁妖塔挑战，成为了"
										+ levelInfo
										+ "中挑战层数最多的强者!";
								MmochatChatService.sendSystemMsg(bestInfo);
							}
							MmochatTaskService.dealFinishingTask(me, task);
						} else {
							if (task.isInvalid()) {
								// 任务已过期
								synchronized (task) {
									if (task.getTaskState() == MmochatTaskStateType.已接) {
										task
												.setTaskState(MmochatTaskStateType.已完成);
									} else {
										continue;
									}
								}
								// 结算奖励
								task.setTargetMapId(task.getTargetMapId() + 1);
								String info = "您完成了锁妖塔"
										+ MmochatTowerService
												.getTowerLevelCount(task)
										+ "层的挑战,获得了";
								if (task.getTaskTag() == MmochatConstant.taskTag_挑战经验通天塔) {
									int expAward = MmochatTowerService
											.getExpAwardOfTower(task);
									me.addExp(expAward);
									info += MmochatUtil.wrapColor(expAward
											+ "点经验", Color.green)
											+ "，";
									if (pet != null) {
										int petExpAward = MmochatBattleService
												.getRealExp(expAward, me, pet);
										pet.addExp(me, petExpAward);
										info += "您的宠物"
												+ pet.getName()
												+ "获得了"
												+ MmochatUtil.wrapColor(
														petExpAward + "点经验",
														Color.green) + "，";
									}
								} else {
									int daoAward = MmochatTowerService
											.getDaoAwardOfTower(task);
									// 刷道比赛
									MmochatPropService.dealDaoActivity(me,
											daoAward);

									int trueDao = MmochatBattleService
											.getRealDao(me, daoAward);
									me.addDao(trueDao);
									info += MmochatUtil.wrapColor(trueDao
											+ "天道行(削减前" + daoAward + "天)",
											Color.green)
											+ "，";
									if (pet != null) {
										int petDao = MmochatBattleService
												.getRealDaoOfLevel(daoAward,
														me, pet);
										int truePetDao = MmochatBattleService
												.getRealDao(pet, petDao);
										pet.addDao(truePetDao);
										info += "您的宠物"
												+ pet.getName()
												+ "获得了"
												+ MmochatUtil.wrapColor(
														truePetDao + "点武学",
														Color.green) + "，";
									}
								}
								info += "今天的挑战已完成!";
								MmochatFriendService
										.sendSystemMsgToPersonByFriendMessage(
												me, info);
								MmochatChatService.sendSystemMsgToPerson(me,
										info);
								// 称号判断与处理，NPC是否替换
								boolean ret = MmochatTowerService
										.dealTowerBest(me, task);
								if (ret) {
									String bestInfo = MmochatUtil.wrapColor(me
											.getName(), Color.green)
											+ "完成了"
											+ MmochatUtil
													.wrapColor(
															MmochatTowerService
																	.getTowerLevelCount(task)
																	+ "层",
															Color.yellow)
											+ "的锁妖塔挑战，成为了"
											+ levelInfo
											+ "中挑战层数最多的强者!";
									MmochatChatService.sendSystemMsg(bestInfo);
								}
								MmochatTaskService.dealFinishingTask(me, task);
							} else {
								// 更新任务
								long startTime = task.getAccept_time();
								synchronized (task) {
									if (task.getTaskState() == MmochatTaskStateType.已接) {
										task
												.setTaskState(MmochatTaskStateType.已完成);
									} else {
										continue;
									}
								}
								// 修改任务
								task.setTargetMapId(task.getTargetMapId() + 1);
								task.setFinish_npcId(MmochatTowerService
										.getTowerBaseNpcIdByMapId(task
												.getTargetMapId()));
								int level = task.getTargetMapId()
										- MmochatConstant.mapId_锁妖塔20级
										+ MmochatConstant.minTowerLevel;
								task.setTaskState(MmochatTaskStateType.已接);
								task.setAccept_time(startTime);
								task
										.setTaskDetail(MmochatTowerService
												.getTowerTaskDetailByLevel(
														level, task));
								// 更新任务列表
								Mmochat_4_11S pack1 = new Mmochat_4_11S();
								pack1.setActionType(1);
								pack1.setMe(me);
								pack1.setTask(task);
								MmochatUtil.sendCommonPack(me, pack1);

								MmochatChatService.sendSystemMsgToPerson(me,
										"您已经完成本层挑战，现在可以通过传送人传送到下一层进行挑战了!");

								// 道具:血池法池效果
								MmochatPropService.useAutoAddBloodSpritProp(me);

								// 更新头像图标信息
								Mmochat_1_7S pack = new Mmochat_1_7S(false);
								pack.setMe(me);
								MmochatUtil.sendCommonPack(me, pack);

								// 放入队列中
								if (!MmochatTaskService.needToSaveTaskSqlPlayerQueue
										.contains(me)
										&& !MmochatGmService.isSystemExit()) {
									MmochatTaskService.needToSaveTaskSqlPlayerQueue
											.add(me);
								}
								continue;
							}
						}

						// 存数据库
						try {
							MmochatDao.saveExpAndDao(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				// 输了
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}

						MmochatTask task = me
								.getTaskByType(MmochatTaskType.挑战锁妖塔);
						if (task == null
								|| task.getTaskState() != MmochatTaskStateType.已接) {
							continue;
						}
						synchronized (task) {
							if (task.getTaskState() == MmochatTaskStateType.已接) {
								task.setTaskState(MmochatTaskStateType.已完成);
							} else {
								continue;
							}
						}
						// 结算奖励
						MmochatPet pet = me.getPkPet();
						String info = "您完成了锁妖塔"
								+ MmochatTowerService.getTowerLevelCount(task)
								+ "层的挑战,获得了";
						if (task.getTaskTag() == MmochatConstant.taskTag_挑战经验通天塔) {
							int expAward = MmochatTowerService
									.getExpAwardOfTower(task);
							me.addExp(expAward);
							info += MmochatUtil.wrapColor(expAward + "点经验",
									Color.green)
									+ "，";
							if (pet != null) {
								int petExpAward = MmochatBattleService
										.getRealExp(expAward, me, pet);
								pet.addExp(me, petExpAward);
								info += "您的宠物"
										+ pet.getName()
										+ "获得了"
										+ MmochatUtil.wrapColor(petExpAward
												+ "点经验", Color.green) + "，";
							}
						} else {
							int daoAward = MmochatTowerService
									.getDaoAwardOfTower(task);
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, daoAward);

							int trueDao = MmochatBattleService.getRealDao(me,
									daoAward);
							me.addDao(trueDao);
							info += MmochatUtil.wrapColor(trueDao + "天道行(削减前"
									+ daoAward + "天)", Color.green)
									+ "，";
							if (pet != null) {
								int petDao = MmochatBattleService
										.getRealDaoOfLevel(daoAward, me, pet);
								int truePetDao = MmochatBattleService
										.getRealDao(pet, petDao);
								pet.addDao(truePetDao);
								info += "您的宠物"
										+ pet.getName()
										+ "获得了"
										+ MmochatUtil.wrapColor(truePetDao
												+ "点武学", Color.green) + "，";
							}
						}
						info += "今天的挑战已完成!";
						MmochatFriendService
								.sendSystemMsgToPersonByFriendMessage(me, info);
						MmochatChatService.sendSystemMsgToPerson(me, info);
						// 称号判断与处理，NPC是否替换
						boolean ret = MmochatTowerService.dealTowerBest(me,
								task);
						if (ret) {
							int lifeTurn = (task.getTaskLevel()
									+ MmochatConstant.startTowerLevelReduce - 120) / 10;
							if (lifeTurn < 0) {
								lifeTurn = 0;
							}
							int pureLevel = task.getTaskLevel()
									+ MmochatConstant.startTowerLevelReduce
									- lifeTurn * 10;
							String levelInfo = "";
							if (lifeTurn > 0) {
								levelInfo += lifeTurn + "转";
							}
							levelInfo += pureLevel + "级";

							String bestInfo = MmochatUtil.wrapColor(me
									.getName(), Color.green)
									+ "完成了"
									+ MmochatUtil.wrapColor(MmochatTowerService
											.getTowerLevelCount(task)
											+ "层", Color.yellow)
									+ "的锁妖塔挑战，成为了"
									+ levelInfo + "中挑战层数最多的强者!";
							MmochatChatService.sendSystemMsg(bestInfo);
						}
						// 存数据库
						MmochatTaskService.dealFinishingTask(me, task);
					}
				}
			}
			break;
		}
		case 挑战纪念兽: {
			if (winTeam == leftTeam) {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}

						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S(false);
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 获得一个纪念蛋
						MmochatProp prop = new MmochatProp(
								MmochatPropType.一周年纪念蛋);
						prop.setCanDeal(false);
						boolean ret = me.addObjectToPackage(prop);
						if (ret) {
							MmochatChatService.sendSystemMsgToPerson(me,
									"在驱赶纪念兽的过程中，你意外得捡到一个"
											+ MmochatUtil.wrapColor(
													MmochatPropType.一周年纪念蛋
															.toString(),
													Color.yellow)
											+ "，快去"
											+ MmochatUtil.wrapColor("西郊",
													Color.green) + "进行孵化吧!");
						} else {
							MmochatChatService.sendSystemMsgToPerson(me,
									"您的包裹已满，无法获得"
											+ MmochatUtil.wrapColor(
													MmochatPropType.一周年纪念蛋
															.toString(),
													Color.yellow) + "!");
						}

						// 存数据库
						try {
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				// 输了，没有损失
			}
			break;
		}
		case 驱赶年兽: {
			if (winTeam == leftTeam) {
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer me = (MmochatPlayer) role;

						me.setCurBlood(Math.max(1, me.getCurPkBlood()));
						me.setCurSprit(Math.max(0, me.getCurPkSprit()));

						for (MmochatPet pet1 : me.getPets().values()) {
							pet1.setCurBlood(Math.max(1, pet1.getCurPkBlood()));
							pet1.setCurSprit(Math.max(0, pet1.getCurPkSprit()));
						}

						// 道具:血池法池效果
						MmochatPropService.useAutoAddBloodSpritProp(me);

						// 更新头像图标信息
						Mmochat_1_7S pack = new Mmochat_1_7S(false);
						pack.setMe(me);
						MmochatUtil.sendCommonPack(me, pack);

						// 获得一个年兽蛋
						MmochatProp prop = new MmochatProp(MmochatPropType.年兽蛋);
						prop.setCanDeal(false);
						boolean ret = me.addObjectToPackage(prop);
						if (ret) {
							MmochatChatService.sendSystemMsgToPerson(me,
									"在驱赶年兽的过程中，你意外得捡到一个"
											+ MmochatUtil.wrapColor(
													MmochatPropType.年兽蛋
															.toString(),
													Color.yellow)
											+ "，快去"
											+ MmochatUtil.wrapColor("西郊",
													Color.green) + "进行孵化吧!");
						} else {
							MmochatChatService.sendSystemMsgToPerson(me,
									"您的包裹已满，无法获得"
											+ MmochatUtil.wrapColor(
													MmochatPropType.年兽蛋
															.toString(),
													Color.yellow) + "!");
						}

						// 存数据库
						try {
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				// 输了，没有损失
			}
			break;
		}
		case 击杀无奖励临时怪物: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));
							for (MmochatPet pet : me.getPets().values()) {
								pet.setCurBlood(Math
										.max(1, pet.getCurPkBlood()));
								pet.setCurSprit(Math
										.max(0, pet.getCurPkSprit()));
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;

		}
		case 击杀血魔副本之树妖: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				MmochatGang gang = null;
				int totalBuildAward = 0;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 帮贡、帮派资金奖励
							// 帮贡奖励
							if (me.hasGang()) {
								gang = MmochatGangService.getGangById(me
										.getGangId());
								if (gang != null) {
									MmochatGangMember myInfo = gang
											.getMembers().get(me.getRoleId());
									if (myInfo != null) {
										myInfo.addBuildValue(50);
										gang.addBuildValue(50);
										totalBuildAward += 50;
										msg += "你获得了50点帮派贡献度、建设度!";
									}
								}
							}

							// 技能书掉落
							if (MmochatUtil.isInPercent(40)) {
								MmochatPropType[] propList = new MmochatPropType[] {
										MmochatPropType.横扫千军秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.道法无边秘籍,
										MmochatPropType.秘籍技能时间书 };
								int index = MmochatUtil
										.getRandomValue(propList.length);
								MmochatProp prop = new MmochatProp(
										propList[index]);
								boolean ret = me.addObjectToPackage(prop);
								if (ret) {
									msg += "你获得了1本"
											+ propList[index].toString() + "!";
								} else {
									msg += "你的包裹已满，无法获得物品奖励!";
								}
							}

							int daoAward = 200;
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, daoAward);

							int trueDao = MmochatBattleService.getRealDao(me,
									daoAward);
							me.addDao(trueDao);
							msg += "你获得了" + trueDao + "天道行(削减前" + daoAward
									+ "天)!";
							if (pet != null) {
								int petDao = MmochatBattleService
										.getRealDaoOfLevel(daoAward, me, pet);
								int truePetDao = MmochatBattleService
										.getRealDao(pet, petDao);
								pet.addDao(truePetDao);
								msg += "您的宠物" + pet.getName() + "获得了"
										+ truePetDao + "点武学!";
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				// 保存帮派建设度
				try {
					if (gang != null) {
						MmochatDao
								.updateAfterGangTask(gang, totalBuildAward, 0);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				// 判断帮派副本是否进入第二阶段
				if (gang != null) {
					MmochatNpcService.cleanTimeOutGangFuBen(gang);
					MmochatGangFuBen fuBen = MmochatNpcService.gangFuBens
							.get(gang.getFuBenId());
					if (fuBen != null && fuBen.getStep() == 1) {
						MmochatMap map = MmochatMapService.maps.get(gang
								.getMapId());
						if (map != null) {
							int totalMonsterNum = (int) (fuBen.getHardParam() * MmochatConstant.monsterNumOfGangFuBen1Step1);
							int restMonsterNum = 0; // 剩下树妖的数量
							for (MmochatNpc npc : map.getLineNpcMap(
									fuBen.getLine()).values()) {
								if (npc.getName().equals("树怪")) {
									restMonsterNum++;
								}
							}
							if (restMonsterNum < totalMonsterNum * 0.4) {
								synchronized (fuBen) {
									if (fuBen.getStep() == 1) {
										fuBen.setStep(2);
										// 放第二波怪
										int minX = 48, maxX = 382;
										int minY = 168, maxY = 444;
										int monsterNum = (int) fuBen
												.getHardParam()
												* MmochatConstant.monsterNumOfGangFuBen1Step2;
										for (int i = 0; i < monsterNum * 0.6; i++) {
											MmochatTempNpc monster = new MmochatTempNpc(
													true);
											monster.setLifeTime(fuBen
													.getRestLife());
											monster.setLine(fuBen.getLine());
											monster.setMapId(gang.getMapId());
											monster
													.setX((short) MmochatUtil
															.getRandomValue(
																	minX, maxX));
											monster
													.setY((short) MmochatUtil
															.getRandomValue(
																	minY, maxY));
											monster.setName("大力虎");
											monster
													.setBmpType(MmochatRoleBmpType.动物);
											monster.getFunctionItems().add(
													MmochatNpcItemType.击杀大力虎);
											MmochatNpcService.npcs.put(monster
													.getNpcId(), monster);
											map
													.addNpc(fuBen.getLine(),
															monster);
										}
										for (int i = 0; i < monsterNum * 0.4; i++) {
											MmochatTempNpc monster = new MmochatTempNpc(
													true);
											monster.setLifeTime(fuBen
													.getRestLife());
											monster.setLine(fuBen.getLine());
											monster.setMapId(gang.getMapId());
											monster
													.setX((short) MmochatUtil
															.getRandomValue(
																	minX, maxX));
											monster
													.setY((short) MmochatUtil
															.getRandomValue(
																	minY, maxY));
											monster.setName("白发魔女");
											monster
													.setBmpType(MmochatRoleBmpType.鬼);
											monster.getFunctionItems().add(
													MmochatNpcItemType.击杀白发魔女);
											MmochatNpcService.npcs.put(monster
													.getNpcId(), monster);
											map
													.addNpc(fuBen.getLine(),
															monster);
										}
										// 通知帮派成员
										String msg = "本帮的"
												+ MmochatUtil.wrapColor(fuBen
														.getType().toString(),
														Color.magenta)
												+ "已进入第"
												+ fuBen.getStep()
												+ "阶段,"
												+ "大力虎、白发魔女出现在帮派地图"
												+ MmochatUtil.wrapColor((fuBen
														.getLine() + 1)
														+ "线", Color.green)
												+ "，请大家合力前往击杀!";
										msg = MmochatUtil.wrapColor(msg,
												Color.yellow);
										MmochatGangService.sendGangSystemMsg(
												gang, msg);
									}

								}
							}
						}
					}
				}

				// 副本完成检测
				if (gang != null) {
					MmochatNpcService.cleanTimeOutGangFuBen(gang);
					MmochatGangFuBen fuBen = MmochatNpcService.gangFuBens
							.get(gang.getFuBenId());
					if (fuBen != null && fuBen.getStep() == 3) {
						MmochatMap map = MmochatMapService.maps.get(gang
								.getMapId());
						if (map != null) {
							int restMonsterNum = 0; // 剩下妖的数量
							for (MmochatNpc npc : map.getLineNpcMap(
									fuBen.getLine()).values()) {
								if (npc.getName().equals("树怪")
										|| npc.getName().equals("大力虎")
										|| npc.getName().equals("白发魔女")) {
									restMonsterNum++;
								}
							}
							if (restMonsterNum == 0) {
								synchronized (fuBen) {
									// 副本完成
									if (fuBen.getStep() == 3) {
										fuBen.setStep(4);
										String msg = "在本帮成员的齐心协力下，本次的"
												+ fuBen.getType()
												+ "已顺利完成!本帮额外获得"
												+ MmochatConstant.gangBuildAwardForFuBen1
												+ "帮派建议度和"
												+ MmochatUtil
														.getColorMoney(MmochatConstant.gangMoneyAwardForFuBen1)
												+ "帮派资金!";
										msg = MmochatUtil.wrapColor(msg,
												Color.yellow);
										MmochatGangService.sendGangSystemMsg(
												gang, msg);

										fuBen.close();
										MmochatNpcService.gangFuBens
												.remove(fuBen.getId());

										gang
												.addBuildValue(MmochatConstant.gangBuildAwardForFuBen1);
										gang
												.addMoney(MmochatConstant.gangMoneyAwardForFuBen1);
										try {
											MmochatDao
													.updateGangBuildAndMoney(
															MmochatConstant.gangBuildAwardForFuBen1,
															MmochatConstant.gangMoneyAwardForFuBen1,
															gang.getId());
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}
						}
					}
				}
			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;

		}
		case 击杀血魔副本之大力虎:
		case 击杀血魔副本之白发魔女: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				int totalBuildAward = 0;
				MmochatGang gang = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 帮贡、帮派资金奖励
							// 帮贡奖励
							if (me.hasGang()) {
								gang = MmochatGangService.getGangById(me
										.getGangId());
								if (gang != null) {
									MmochatGangMember myInfo = gang
											.getMembers().get(me.getRoleId());
									if (myInfo != null) {
										myInfo.addBuildValue(100);
										gang.addBuildValue(100);
										totalBuildAward += 100;
										msg += "你获得了100点帮派贡献度、建设度!";
									}
								}
							}

							// 技能书掉落
							if (MmochatUtil.isInPercent(60)) {
								MmochatPropType[] propList = new MmochatPropType[] {
										MmochatPropType.横扫千军秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.道法无边秘籍,
										MmochatPropType.秘籍技能时间书 };
								int index = MmochatUtil
										.getRandomValue(propList.length);
								MmochatProp prop = new MmochatProp(
										propList[index]);
								boolean ret = me.addObjectToPackage(prop);
								if (ret) {
									msg += "你获得了1本"
											+ propList[index].toString() + "!";
								} else {
									msg += "你的包裹已满，无法获得物品奖励!";
								}
							}

							int daoAward = 200;
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, daoAward);

							int trueDao = MmochatBattleService.getRealDao(me,
									daoAward);
							me.addDao(trueDao);
							msg += "你获得了" + trueDao + "天道行(削减前" + daoAward
									+ "天)!";
							if (pet != null) {
								int petDao = MmochatBattleService
										.getRealDaoOfLevel(daoAward, me, pet);
								int truePetDao = MmochatBattleService
										.getRealDao(pet, petDao);
								pet.addDao(truePetDao);
								msg += "您的宠物" + pet.getName() + "获得了"
										+ truePetDao + "点武学!";
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				// 保存帮派建设度
				try {
					if (gang != null) {
						MmochatDao
								.updateAfterGangTask(gang, totalBuildAward, 0);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				// 判断帮派副本是否进入第三阶段
				if (gang != null) {
					MmochatNpcService.cleanTimeOutGangFuBen(gang);
					MmochatGangFuBen fuBen = MmochatNpcService.gangFuBens
							.get(gang.getFuBenId());
					if (fuBen != null && fuBen.getStep() == 2) {
						MmochatMap map = MmochatMapService.maps.get(gang
								.getMapId());
						if (map != null) {
							int totalMonsterNum = (int) (fuBen.getHardParam() * MmochatConstant.monsterNumOfGangFuBen1Step2);
							int restMonsterNum = 0; // 剩下妖的数量
							for (MmochatNpc npc : map.getLineNpcMap(
									fuBen.getLine()).values()) {
								if (npc.getName().equals("大力虎")
										|| npc.getName().equals("白发魔女")) {
									restMonsterNum++;
								}
							}
							if (restMonsterNum < totalMonsterNum * 0.5) {
								synchronized (fuBen) {
									if (fuBen.getStep() == 2) {
										fuBen.setStep(3);
										// 放第三波怪
										int minX = 48, maxX = 382;
										int minY = 168, maxY = 444;
										{
											MmochatTempNpc monster = new MmochatTempNpc(
													true);
											monster.setLifeTime(fuBen
													.getRestLife());
											monster.setLine(fuBen.getLine());
											monster.setMapId(gang.getMapId());
											monster
													.setX((short) MmochatUtil
															.getRandomValue(
																	minX, maxX));
											monster
													.setY((short) MmochatUtil
															.getRandomValue(
																	minY, maxY));
											monster.setName("血魔");
											monster
													.setBmpType(MmochatRoleBmpType.祖师爷);
											monster.getFunctionItems().add(
													MmochatNpcItemType.击杀血魔);
											MmochatNpcService.npcs.put(monster
													.getNpcId(), monster);
											map
													.addNpc(fuBen.getLine(),
															monster);
										}

										// 通知帮派成员
										String msg = "本帮的"
												+ MmochatUtil.wrapColor(fuBen
														.getType().toString(),
														Color.magenta)
												+ "已进入第"
												+ fuBen.getStep()
												+ "阶段,"
												+ "终极BOSS血魔已现身帮派地图"
												+ MmochatUtil.wrapColor((fuBen
														.getLine() + 1)
														+ "线", Color.green)
												+ "，请帮内高手组队前往击杀!";
										msg = MmochatUtil.wrapColor(msg,
												Color.yellow);
										MmochatGangService.sendGangSystemMsg(
												gang, msg);
									}

								}
							}
						}
					}
				}

				// 副本完成检测

				if (gang != null) {
					MmochatNpcService.cleanTimeOutGangFuBen(gang);
					MmochatGangFuBen fuBen = MmochatNpcService.gangFuBens
							.get(gang.getFuBenId());
					if (fuBen != null && fuBen.getStep() == 3) {
						MmochatMap map = MmochatMapService.maps.get(gang
								.getMapId());
						if (map != null) {
							int restMonsterNum = 0; // 剩下妖的数量
							for (MmochatNpc npc : map.getLineNpcMap(
									fuBen.getLine()).values()) {
								if (npc.getName().equals("树怪")
										|| npc.getName().equals("大力虎")
										|| npc.getName().equals("白发魔女")) {
									restMonsterNum++;
								}
							}
							if (restMonsterNum == 0) {
								synchronized (fuBen) {
									// 副本完成
									if (fuBen.getStep() == 3) {
										fuBen.setStep(4);
										String msg = "在本帮成员的齐心协力下，本次的"
												+ fuBen.getType()
												+ "已顺利完成!本帮额外获得"
												+ MmochatConstant.gangBuildAwardForFuBen1
												+ "帮派建议度和"
												+ MmochatUtil
														.getColorMoney(MmochatConstant.gangMoneyAwardForFuBen1)
												+ "帮派资金!";
										msg = MmochatUtil.wrapColor(msg,
												Color.yellow);
										MmochatGangService.sendGangSystemMsg(
												gang, msg);

										fuBen.close();
										MmochatNpcService.gangFuBens
												.remove(fuBen.getId());

										gang
												.addBuildValue(MmochatConstant.gangBuildAwardForFuBen1);
										gang
												.addMoney(MmochatConstant.gangMoneyAwardForFuBen1);
										try {
											MmochatDao
													.updateGangBuildAndMoney(
															MmochatConstant.gangBuildAwardForFuBen1,
															MmochatConstant.gangMoneyAwardForFuBen1,
															gang.getId());
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}
						}
					}
				}

			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;

		}
		case 击杀血魔副本之终极BOSS: {
			MmochatTempNpc boss = null;
			MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
			if (tmpNpc != null) {
				if (tmpNpc instanceof MmochatTempNpc) {
					boss = (MmochatTempNpc) tmpNpc;
				}
			}
			if (boss == null) {
				break;
			}
			if (winTeam == leftTeam) {
				// 去除临时NPC,并设置NPC为离开
				MmochatMap theMap = MmochatMapService.maps.get(tmpNpc
						.getMapId());
				if (theMap != null) {
					theMap.delNpc(boss.getLine(), tmpNpc);
				}
				boss.setState(MmochatNpcState.离开);

				// 增加好友度
				Map<Integer, MmochatPlayer> players = new ConcurrentHashMap<Integer, MmochatPlayer>();
				for (MmochatRole r : leftTeam.values()) {
					if (r instanceof MmochatPlayer) {
						MmochatPlayer m = (MmochatPlayer) r;
						players.put(m.getRoleId(), m);
					}
				}
				MmochatFriendService.addFriendValue(players.values(), 1);

				// 发奖励
				int totalBuildAward = 0;
				MmochatGang gang = null;
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						try {
							String msg = "";
							MmochatPlayer me = (MmochatPlayer) role;
							me.setCurBlood(Math.max(1, me.getCurPkBlood()));
							me.setCurSprit(Math.max(0, me.getCurPkSprit()));

							MmochatPet pet = me.getPetInBattle();
							if (pet != null && pet.getCurBlood() <= 0) {
								pet = null;
							}

							for (MmochatPet pet1 : me.getPets().values()) {
								pet1.setCurBlood(Math.max(1, pet1
										.getCurPkBlood()));
								pet1.setCurSprit(Math.max(0, pet1
										.getCurPkSprit()));
							}

							// 帮贡、帮派资金奖励
							// 帮贡奖励
							if (me.hasGang()) {
								gang = MmochatGangService.getGangById(me
										.getGangId());
								if (gang != null) {
									MmochatGangMember myInfo = gang
											.getMembers().get(me.getRoleId());
									if (myInfo != null) {
										myInfo.addBuildValue(300);
										gang.addBuildValue(300);
										totalBuildAward += 300;
										msg += "你获得了300点帮派贡献度、建设度!";
									}
								}
							}

							// 技能书掉落
							for (int i = 0; i < 2; i++) {
								MmochatPropType[] propList = new MmochatPropType[] {
										MmochatPropType.横扫千军秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.一击必杀秘籍,
										MmochatPropType.道法无边秘籍,
										MmochatPropType.秘籍技能时间书 };
								int index = MmochatUtil
										.getRandomValue(propList.length);
								MmochatProp prop = new MmochatProp(
										propList[index]);
								boolean ret = me.addObjectToPackage(prop);
								if (ret) {
									msg += "你获得了1本"
											+ propList[index].toString() + "!";
								} else {
									msg += "你的包裹已满，无法获得物品奖励!";
								}
							}

							int daoAward = 500;
							// 刷道比赛
							MmochatPropService.dealDaoActivity(me, daoAward);

							int trueDao = MmochatBattleService.getRealDao(me,
									daoAward);
							me.addDao(trueDao);
							msg += "你获得了" + trueDao + "天道行(削减前" + daoAward
									+ "天)!";
							if (pet != null) {
								int petDao = MmochatBattleService
										.getRealDaoOfLevel(daoAward, me, pet);
								int truePetDao = MmochatBattleService
										.getRealDao(pet, petDao);
								pet.addDao(truePetDao);
								msg += "您的宠物" + pet.getName() + "获得了"
										+ truePetDao + "点武学!";
							}

							// 道具:血池法池效果
							MmochatPropService.useAutoAddBloodSpritProp(me);

							// 更新头像图标信息
							Mmochat_1_7S pack = new Mmochat_1_7S(false);
							pack.setMe(me);
							MmochatUtil.sendCommonPack(me, pack);

							if (msg != "") {
								MmochatChatService.sendSystemMsgToPerson(me,
										msg);
							}
							// 保存数据库
							MmochatBattleService.saveSqlAfterBattle(me);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				// 保存帮派建设度
				try {
					if (gang != null) {
						MmochatDao
								.updateAfterGangTask(gang, totalBuildAward, 0);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				// 副本完成检测
				if (gang != null) {
					MmochatNpcService.cleanTimeOutGangFuBen(gang);
					MmochatGangFuBen fuBen = MmochatNpcService.gangFuBens
							.get(gang.getFuBenId());
					if (fuBen != null && fuBen.getStep() == 3) {
						MmochatMap map = MmochatMapService.maps.get(gang
								.getMapId());
						if (map != null) {
							int restMonsterNum = 0; // 剩下妖的数量
							for (MmochatNpc npc : map.getLineNpcMap(
									fuBen.getLine()).values()) {
								if (npc.getName().equals("树怪")
										|| npc.getName().equals("大力虎")
										|| npc.getName().equals("白发魔女")) {
									restMonsterNum++;
								}
							}
							if (restMonsterNum == 0) {
								synchronized (fuBen) {
									// 副本完成
									if (fuBen.getStep() == 3) {
										fuBen.setStep(4);
										String msg = "在本帮成员的齐心协力下，本次的"
												+ fuBen.getType()
												+ "已顺利完成!本帮额外获得"
												+ MmochatConstant.gangBuildAwardForFuBen1
												+ "帮派建议度和"
												+ MmochatUtil
														.getColorMoney(MmochatConstant.gangMoneyAwardForFuBen1)
												+ "帮派资金!";
										msg = MmochatUtil.wrapColor(msg,
												Color.yellow);
										MmochatGangService.sendGangSystemMsg(
												gang, msg);

										fuBen.close();
										MmochatNpcService.gangFuBens
												.remove(fuBen.getId());

										gang
												.addBuildValue(MmochatConstant.gangBuildAwardForFuBen1);
										gang
												.addMoney(MmochatConstant.gangMoneyAwardForFuBen1);
										try {
											MmochatDao
													.updateGangBuildAndMoney(
															MmochatConstant.gangBuildAwardForFuBen1,
															MmochatConstant.gangMoneyAwardForFuBen1,
															gang.getId());
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}
							}
						}
					}
				}

			} else {
				boss.setState(MmochatNpcState.空闲);
				for (MmochatRole role : leftTeam.values()) {
					if (role instanceof MmochatPlayer) {
						// 玩家输了
						// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
						MmochatPlayer me = (MmochatPlayer) role;
						me.setDeadInBattle(true);
					}
				}
			}
			break;
		}
		case 凤鸣岐山:
		case 哪吒闹海:
		case 普通任务:
		case 新手指引:
		case 每日大众任务:
		case 帮派任务:
		default: {
			// 根据任务xml文件的配置来发送奖励
			MmochatPlayer tasker = MmochatMainService
					.getRoleFromBuffer(taskBattle_roleId);
			if (tasker == null) {
				// 已经掉线
				break;
			}
			Byte rolePos = getPos(tasker);
			if (rolePos == null) {
				// 已经逃跑
				break;
			}
			MmochatTask myTask = tasker.getTaskById(taskBattle_taskId);
			if (myTask == null || !myTask.canFinish(tasker)) {
				// 任务不存在或未完成
				break;
			}
			MmochatNpcBattle battle = myTask.getFinish_need_battles();
			if (battle == null) {
				break;
			}
			if (battle.isMustWin()) {
				// 必需胜利才能完成任务
				if (winTeam == leftTeam) {
					if (!battle.isFinishTeamTask()) {
						MmochatTaskService.dealFinishingTask(tasker, myTask);
					} else {
						// 完成全队人任务
						MmochatTaskType taskType = myTask.getTaskType();
						for (MmochatRole role : leftTeam.values()) {
							if (role instanceof MmochatPlayer) {
								MmochatPlayer me = (MmochatPlayer) role;
								MmochatTask task = me.getTaskByType(taskType);
								if (task != null
										&& task.getTaskState() != MmochatTaskStateType.已完成
										&& task.canFinish(me)) {
									MmochatTaskService.dealFinishingTask(me,
											task);
								}
							}
						}
					}
				} else {
					for (MmochatRole role : leftTeam.values()) {
						if (role instanceof MmochatPlayer) {
							// 玩家输了
							// TODO:将玩家转移到出生地，并扣除相应死亡惩罚
							MmochatPlayer me = (MmochatPlayer) role;
							me.setDeadInBattle(true);
						}
					}
				}
			} else {
				// 不论输赢，都完成任务
				MmochatTaskService.dealFinishingTask(tasker, myTask);
			}
			break;
		}
		}
	}

	// 战斗结束
	public void closeBattle() {
		log.debug("closeBattle");
		setState(MmochatBattleState.战斗结束);
		try {
			for (MmochatRole role : roles.values()) {
				try {
					if (role instanceof MmochatPlayer) {
						// 人物
						MmochatPlayer p = (MmochatPlayer) role;
						p.leaveBattle();
						for (MmochatPet pet : p.getPets().values()) {
							pet.leaveBattle();
						}
						MmochatMainService.dealDead(p);
						if (p.isDeadConnection()) {
							// 离开游戏
							MmochatMainService.leaveGame(p);
						}
					} else if (role instanceof MmochatPet) {
					} else if (role instanceof MmochatMonster) {
						// 怪物,BOSS
						MmochatMonster p = (MmochatMonster) role;
						p.leaveBattle();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			for (MmochatPlayer role : audiences.values()) {
				role.unLockState();
				role.leaveWatchingBattle();
				if (role.isDeadConnection()) {
					// 离开游戏
					MmochatMainService.leaveGame(role);
				}
			}

			// 设置临时NPC状态
			if (tmpNpcId < 0) {
				MmochatNpc tmpNpc = MmochatNpcService.npcs.get(tmpNpcId);
				if (tmpNpc != null) {
					if (tmpNpc instanceof MmochatTempNpc) {
						MmochatTempNpc boss = (MmochatTempNpc) tmpNpc;
						if (boss.getState() == MmochatNpcState.战斗中) {
							boss.setState(MmochatNpcState.空闲);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			audiences.clear();
			roles.clear();
			leftTeam.clear();
			rightTeam.clear();
			actions.clear();
			aniOverMap.clear();
			roundNewStates.clear();
			frames.clear();
			newRoles_changeState.clear();
			newRoles_delRole.clear();
			newRoles_addRole.clear();
			winTeam.clear();
			loseTeam.clear();
			killedMonster.clear();
		} catch (Exception e) {
			e.printStackTrace();
		}

		MmochatBattleService.battles.remove(battleId);
	}

	public static byte getPetPos(int manPos) {
		byte[] leftPetPos = new byte[] { 5, 6, 7, 8, 9 };
		byte[] rightPetPos = new byte[] { 10, 11, 12, 13, 14 };
		if (manPos < 5) {
			return leftPetPos[manPos];
		} else {
			return rightPetPos[manPos - 15];
		}
	}

	public static byte getManPos(int petPos) {
		byte[] leftManPos = new byte[] { 0, 1, 2, 3, 4 };
		byte[] rightManPos = new byte[] { 15, 16, 17, 18, 19 };
		if (petPos < 10) {
			return leftManPos[petPos - 5];
		} else {
			return rightManPos[petPos - 10];
		}
	}

	public void addLeftTeam(MmochatRole role, boolean initBlood) {
		if (role == null) {
			return;
		}
		if (role instanceof MmochatPlayer) {
			int[] pos = new int[] { 2, 1, 3, 0, 4 };
			int[] petPos = new int[] { 7, 6, 8, 5, 9 };
			int leftPersonNum = 0;
			for (Entry<Byte, MmochatRole> entry : leftTeam.entrySet()) {
				if (entry.getKey() < MmochatConstant.rowNumInPk) {
					leftPersonNum++;
				}
			}
			MmochatPlayer player = (MmochatPlayer) role;
			if (leftPersonNum < MmochatConstant.rowNumInPk) {
				leftTeam.put((byte) pos[leftPersonNum], role);
				roles.put((byte) pos[leftPersonNum], role);
				role.initPkState(battleId, initBlood);
				// 参战宠物
				MmochatPet pet = player.getPkPet();
				if (pet != null) {
					leftTeam.put((byte) petPos[leftPersonNum], pet);
					roles.put((byte) petPos[leftPersonNum], pet);
					// pet.initPkState(battleId, initBlood);
				}
				for (MmochatPet myPet : player.getPets().values()) {
					myPet.initPkState(battleId, initBlood);
				}
			}
		} else {
			int[] pos = new int[] { 2, 1, 3, 0, 4, 7, 6, 8, 5, 9 };
			int teamNum = leftTeam.size();
			if (teamNum < 2 * MmochatConstant.rowNumInPk) {
				leftTeam.put((byte) pos[teamNum], role);
				roles.put((byte) pos[teamNum], role);
				role.initPkState(battleId, initBlood);
			}
		}
	}

	public void addRightTeam(MmochatRole role, boolean initBlood) {
		if (role == null) {
			return;
		}
		if (role instanceof MmochatPlayer) {
			int[] pos = new int[] { 17, 16, 18, 15, 19 };
			int[] petPos = new int[] { 12, 11, 13, 10, 14 };
			int rightPersonNum = 0;
			for (Entry<Byte, MmochatRole> entry : rightTeam.entrySet()) {
				if (entry.getKey() >= MmochatConstant.rowNumInPk * 3) {
					rightPersonNum++;
				}
			}
			MmochatPlayer player = (MmochatPlayer) role;
			if (rightPersonNum < MmochatConstant.rowNumInPk) {
				rightTeam.put((byte) pos[rightPersonNum], role);
				roles.put((byte) pos[rightPersonNum], role);
				role.initPkState(battleId, initBlood);
				// 参战宠物
				MmochatPet pet = player.getPkPet();
				if (pet != null) {
					rightTeam.put((byte) petPos[rightPersonNum], pet);
					roles.put((byte) petPos[rightPersonNum], pet);
					// pet.initPkState(battleId, initBlood);
				}
				for (MmochatPet myPet : player.getPets().values()) {
					myPet.initPkState(battleId, initBlood);
				}
			}
		} else {
			int[] pos = new int[] { 17, 16, 18, 15, 19, 12, 11, 13, 10, 14 };
			int teamNum = rightTeam.size();
			if (teamNum < 2 * MmochatConstant.rowNumInPk) {
				rightTeam.put((byte) pos[teamNum], role);
				roles.put((byte) pos[teamNum], role);
				role.initPkState(battleId, initBlood);
			}
		}
	}

	public void addRightPetTeam(MmochatRole pet, boolean initBlood) {
		if (pet == null) {
			return;
		}
		int[] pos = new int[] { 12, 11, 13, 10, 14 };
		for (int i = 0; i < pos.length; i++) {
			int curPos = pos[i];
			if (!rightTeam.containsKey((byte) curPos)) {
				rightTeam.put((byte) curPos, pet);
				roles.put((byte) curPos, pet);
				pet.initPkState(battleId, initBlood);
				break;
			}
		}
	}

	public Byte getPos(MmochatRole role) {
		for (Entry<Byte, MmochatRole> entry : roles.entrySet()) {
			if (entry.getValue() == role) {
				return entry.getKey();
			}
		}
		return null;
	}

	// 是否所有玩家的动画播放结束
	public boolean isAllAniOver() {
		for (MmochatRole role : roles.values()) {
			if (role instanceof MmochatPlayer) {
				MmochatPlayer me = (MmochatPlayer) role;
				if (!aniOverMap.containsKey(me.getRoleId())) {
					return false;
				}
			}
		}
		return true;
	}

	public Map<Byte, MmochatAction> getActions() {
		return actions;
	}

	public void setActions(Map<Byte, MmochatAction> actions) {
		this.actions = actions;
	}

	public void addAction(MmochatAction action) {
		actions.put(action.getPos(), action);
	}

	public Map<Integer, Integer> getAniOverMap() {
		return aniOverMap;
	}

	public void setAniOverMap(Map<Integer, Integer> aniOverMap) {
		this.aniOverMap = aniOverMap;
	}

	public MmochatBattleState getState() {
		return state;
	}

	public void setState(MmochatBattleState state) {
		if (this.state != state) {
			this.state = state;
			curStateStartTime = System.currentTimeMillis();
			log.debug("战场状态变更为:" + state.toString());
		}
	}

	public int getTaskBattle_taskId() {
		return taskBattle_taskId;
	}

	public void setTaskBattle_taskId(int taskBattleTaskId) {
		taskBattle_taskId = taskBattleTaskId;
	}

	public int getTaskBattle_roleId() {
		return taskBattle_roleId;
	}

	public void setTaskBattle_roleId(int taskBattleRoleId) {
		taskBattle_roleId = taskBattleRoleId;
	}

	public List<MmochatRoundFrame> getFrames() {
		return frames;
	}

	public void setFrames(List<MmochatRoundFrame> frames) {
		this.frames = frames;
	}

	public void addFrame(MmochatRoundFrame frame) {
		frames.add(frame);
	}

	public Map<Byte, MmochatRoundStateChange> getRoundNewStates() {
		return roundNewStates;
	}

	public void setRoundNewStates(
			Map<Byte, MmochatRoundStateChange> roundNewStates) {
		this.roundNewStates = roundNewStates;
	}

	public void addNewRoleState_changeState(byte pos, MmochatRole role) {
		MmochatRoundStateChange state = new MmochatRoundStateChange();
		state.setSmeValue(role.getSmeValue());
		state.setCurPkBlood(role.getCurPkBlood());
		state.setCurPkSprit(role.getCurPkSprit());
		newRoles_changeState.put(pos, state);
	}

	public Map<Byte, MmochatRoundStateChange> getNewRoles_changeState() {
		return newRoles_changeState;
	}

	public void setNewRoles_changeState(
			Map<Byte, MmochatRoundStateChange> newRolesChangeState) {
		newRoles_changeState = newRolesChangeState;
	}

	public void addDefaultAction(MmochatPlayer player) {
		// 添加自动物攻操作
		if (player == null) {
			return;
		}
		if (isOver() == true) {
			return;
		}
		if (getState() != MmochatBattleState.战斗新回合玩家操作中) {
			return;
		}

		Byte myPos = getPos(player);
		if (myPos == null) {
			return;
		}

		byte petPos = getPetPos(myPos);
		MmochatRole petRole = roles.get(petPos);
		if (petRole != null && petRole instanceof MmochatPet) {
			// 有宠物的情况，人宠都操作过了则返回
			if (actions.get(petPos) != null && actions.get(myPos) != null) {
				return;
			}
		} else {
			// 没宠物的情况，人操作过了则返回
			if (actions.get(myPos) != null) {
				return;
			}
		}
		log.debug("添加自动物攻操作");

		Byte targetPos = 0;
		Map<Byte, MmochatRole> hisTeam = getTheOtherTeam(myPos);
		if (hisTeam != null && hisTeam.size() > 0) {
			int size = hisTeam.size();
			int index = (int) (Math.random() * size);
			int curIndex = 0;
			for (Entry<Byte, MmochatRole> entry : hisTeam.entrySet()) {
				if (curIndex == index) {
					targetPos = entry.getKey();
					break;
				}
				curIndex++;
			}

		}

		if (actions.get(myPos) == null) {
			MmochatAction action = new MmochatAction();
			action.setBattleId(battleId);
			action.setTurn((short) turn);
			action.setPos(myPos);
			action.setActionType(MmochatActionType.物攻);
			action.setTargetPos(targetPos);
			action.setRole(player);
			addAction(action);
		}

		if (petRole != null && petRole instanceof MmochatPet) {
			MmochatPet pet = (MmochatPet) petRole;
			if (actions.get(petPos) == null) {
				MmochatAction action = new MmochatAction();
				action.setBattleId(battleId);
				action.setTurn((short) turn);
				action.setPos(petPos);
				action.setActionType(MmochatActionType.物攻);
				action.setTargetPos(targetPos);
				action.setRole(pet);
				addAction(action);
			}
		}

	}

	public void setActionPlayTimePerRound(int frameNum) {
		actionPlayNum = frameNum;
	}

	public void start() {
		initNewRound();
		// 给所有离线玩家和怪物添加自动战斗
		addActionForOfflinePlayerAndMonster();
		setState(MmochatBattleState.战斗新回合玩家操作中);
		MmochatBattleService.checkBattleIsReady(this);
		log.debug("战斗开始，当前状态:" + state.toString());
	}

	// 给所有离线玩家和怪物添加自动战斗
	public void addActionForOfflinePlayerAndMonster() {
		log.debug("给所有离线玩家和怪物添加自动战斗");
		for (MmochatRole role : roles.values()) {
			role.setUseDefendInPk(false);
			if (role instanceof MmochatPlayer) {
				MmochatPlayer player = (MmochatPlayer) role;
				if (player.isDeadConnection()
						|| !MmochatMainService.players.containsKey(player
								.getRoleId())) {
					// 玩家已经掉线,添加自动物攻操作
					addDefaultAction(player);
				} else {
					// 如果是在比赛中，当人和宠都被障碍时，则给他自动添加操作
					switch (type) {
					case 个人争霸赛:
					case 天下第一争霸赛:
					case 武道会比赛:
					case 跨服武道会比赛: {
						if (player.hasSme(MmochatSmeType.冰冻)
								|| player.hasSme(MmochatSmeType.混乱)
								|| player.hasSme(MmochatSmeType.封印)) {
							MmochatPet pkPet = player.getPetInBattle();
							if (pkPet != null) {
								if (pkPet.hasSme(MmochatSmeType.冰冻)
										|| pkPet.hasSme(MmochatSmeType.混乱)
										|| pkPet.hasSme(MmochatSmeType.封印)) {
									addDefaultAction(player);
								}
							}
						}
						break;
					}
					default:
						break;
					}
				}
			} else if (role instanceof MmochatPet) {
				// 宠物
			} else if (role instanceof MmochatMonster) {
				// 怪，执行本回合操作
				MmochatAction action = MmochatBattleService.getMonsterAction(
						(MmochatMonster) role, this);
				if (action != null) {
					addAction(action);
				}
			}
		}
	}

	// 给所有未操作的玩家和怪物添加自动战斗
	public void addActionForUnActionPlayer() {
		log.debug("给所有未操作的玩家和怪物添加自动战斗");
		for (MmochatRole role : roles.values()) {
			role.setUseDefendInPk(false);
			if (role instanceof MmochatPlayer) {
				MmochatPlayer player = (MmochatPlayer) role;
				addDefaultAction(player);
			} else if (role instanceof MmochatPet) {
				// 宠物
			} else if (role instanceof MmochatMonster) {
				// 怪，执行本回合操作
				MmochatAction action = MmochatBattleService.getMonsterAction(
						(MmochatMonster) role, this);
				if (action != null) {
					addAction(action);
				}
			}
		}
	}

	// 给离线的玩家添加动画结束操作
	public void setAniOverForOfflinePlayer() {
		log.debug("给离线的玩家添加动画结束操作");
		for (MmochatRole role : roles.values()) {
			if (role instanceof MmochatPlayer) {
				MmochatPlayer player = (MmochatPlayer) role;
				if (player.isDeadConnection()
						|| !MmochatMainService.players.containsKey(player
								.getRoleId())) {
					// 玩家已经掉线,添加动画结束操作
					getAniOverMap().put(player.getRoleId(), player.getRoleId());
				}
			}
		}
	}

	public void initNewRound() {
		log.debug("initNewRound");
		if (MmochatGmService.isSystemExit()) {
			return;
		}
		actions.clear();
		aniOverMap.clear();
		roundNewStates.clear();
		newRoles_changeState.clear();
		newRoles_delRole.clear();
		newRoles_addRole.clear();
		frames.clear();

		// 给所有离线玩家和怪物添加自动战斗
		// addActionForOfflinePlayerAndMonster();
	}

	public void checkBattleStateTimeOut() {
		if (state == MmochatBattleState.战斗新回合玩家操作中) {
			long optionTime;
			switch (type) {
			case 个人争霸赛:
			case 天下第一争霸赛:
			case 武道会比赛:
			case 跨服武道会比赛: {
				optionTime = MmochatConstant.pkGameOptionTimeLimit;
			}
			default:
				optionTime = MmochatConstant.pkOptionTimeLimit;
			}
			if (System.currentTimeMillis() - curStateStartTime > optionTime
					+ MmochatConstant.netDelay) {
				// 玩家操作超时: 系统添加自动战斗
				log.debug("战斗新回合玩家操作中，玩家操作超时: 系统添加自动战斗");
				addActionForUnActionPlayer();
				MmochatBattleService.checkBattleIsReady(this);
			}
		} else if (state == MmochatBattleState.战斗动画播放中) {
			// 玩家战斗动画播放超时: 下发强制结束动画协议(如果终端没调用本地框，可以忽略此消息)
			log.debug("战斗动画播放中，玩家动画播放超时，结束动画，进入新回合操作");
			if (!isOver()) {
				for (MmochatRole role : roles.values()) {
					if (role instanceof MmochatPlayer) {
						MmochatPlayer player = (MmochatPlayer) role;
						if (player.isDeadConnection()
								|| !MmochatMainService.players
										.containsKey(player.getRoleId())) {
							// 玩家已经掉线
							long timeLimit = actionPlayNum
									* MmochatConstant.pkActionPlayTimeLimitPerRole
									+ MmochatConstant.netDelay;
							if (!aniOverMap.containsKey(player.getRoleId())) {
								if (System.currentTimeMillis()
										- curStateStartTime > timeLimit) {
									aniOverMap.put(player.getRoleId(), player
											.getRoleId());
								}
							}
						} else {
							if (!aniOverMap.containsKey(player.getRoleId())) {
								// 未掉线的玩家中，未上传动画结束的
								long timeLimit;
								switch (type) {
								case 个人争霸赛:
								case 天下第一争霸赛:
								case 武道会比赛:
								case 帮派大乱斗:
								case 强制杀戮:
								case 跨服武道会比赛: {
									timeLimit = actionPlayNum
											* MmochatConstant.pkActionPlayTimeLimitPerRole_max
											+ 2 * MmochatConstant.netDelay;
									break;
								}
								default: {
									// true:正常时间
									// false:缩短时间
									if (player
											.getOtherSystemSettingByType(MmochatOtherSystemSettingType.战斗过程时间)) {
										// 正常时间
										timeLimit = actionPlayNum
												* MmochatConstant.pkActionPlayTimeLimitPerRole
												+ MmochatConstant.netDelay;
									} else {
										// 缩短时间
										timeLimit = actionPlayNum
												* MmochatConstant.pkActionPlayTimeLimit2PerRole
												+ MmochatConstant.netDelay;
									}
									break;
								}
								}

								if (System.currentTimeMillis()
										- curStateStartTime > timeLimit) {
									Mmochat_3_11S pack = new Mmochat_3_11S();
									pack.setBattleId(battleId);
									pack.setTurn(turn - 1);
									MmochatUtil.sendCommonPack(player, pack);
									aniOverMap.put(player.getRoleId(), player
											.getRoleId());
								}
							}
						}
					}
				}
			}

			// 所有人的动画播放结束，进入到新回合
			if (isAllAniOver()) {
				synchronized (this) {
					if (getState() != MmochatBattleState.战斗动画播放中) {
						return;
					}
					setState(MmochatBattleState.战斗新回合玩家操作中);
				}
				// 给所有离线玩家和怪物添加自动战斗
				addActionForOfflinePlayerAndMonster();
				MmochatBattleService.checkBattleIsReady(this);
			}

		} else if (state == MmochatBattleState.战斗即将结束) {
			long timeLimit = actionPlayNum
					* MmochatConstant.pkActionPlayTimeLimit2PerRole;
			log.debug("actionPlayNum=" + actionPlayNum);
			if (System.currentTimeMillis() - curStateStartTime > timeLimit) {
				setState(MmochatBattleState.战斗结束);
				try {
					handleAward();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					closeBattle();
				}
			}
		} else {
			if (System.currentTimeMillis() - curStateStartTime > MmochatConstant.pkRoundTimeLimit) {
				// 战斗异常超时，结束战斗
				log.debug("战斗异常超时");
				closeBattle();
			}
		}

	}

	public long getCurStateStartTime() {
		return curStateStartTime;
	}

	public void setCurStateStartTime(long curStateStartTime) {
		this.curStateStartTime = curStateStartTime;
	}

	public void addTurn() {
		turn++;
	}

	public Map<Byte, MmochatRole> getMyTeam(int myPos) {
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			return leftTeam;
		} else {
			return rightTeam;
		}
	}

	// 获取可以复活的死亡队友
	public Map<Byte, MmochatRole> getCanReLiveDeadTeamers(int myPos) {
		Map<Byte, MmochatRole> myTeam;
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			myTeam = leftTeam;
		} else {
			myTeam = rightTeam;
		}
		Map<Byte, MmochatRole> deadTeamer = new ConcurrentHashMap<Byte, MmochatRole>();
		for (Entry<Byte, MmochatRole> entry : myTeam.entrySet()) {
			if (entry.getValue().getCurPkBlood() <= 0
					&& entry.getValue().isCanRescure()) {
				deadTeamer.put(entry.getKey(), entry.getValue());
			}
		}
		return deadTeamer;
	}

	// 获取我队中气血>0的玩家列表(除自己)
	public Map<Byte, MmochatRole> getMyAliveTeamersExceptMe(int myPos) {
		Map<Byte, MmochatRole> myTeam;
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			myTeam = leftTeam;
		} else {
			myTeam = rightTeam;
		}
		Map<Byte, MmochatRole> aliveTeamer = new ConcurrentHashMap<Byte, MmochatRole>();
		for (Entry<Byte, MmochatRole> entry : myTeam.entrySet()) {
			if (entry.getValue().getCurPkBlood() > 0 && entry.getKey() != myPos) {
				aliveTeamer.put(entry.getKey(), entry.getValue());
			}
		}
		return aliveTeamer;
	}

	// 获取我队中气血>0,且包含sme动画的玩家列表(除自己)
	public Map<Byte, MmochatRole> getMyAliveTeamersExceptMeWithSmeState(
			int myPos, MmochatSmeType sme) {
		Map<Byte, MmochatRole> myTeam;
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			myTeam = leftTeam;
		} else {
			myTeam = rightTeam;
		}
		Map<Byte, MmochatRole> aliveTeamer = new ConcurrentHashMap<Byte, MmochatRole>();
		for (Entry<Byte, MmochatRole> entry : myTeam.entrySet()) {
			if (entry.getValue().getCurPkBlood() > 0 && entry.getKey() != myPos
					&& entry.getValue().hasSme(sme)) {
				aliveTeamer.put(entry.getKey(), entry.getValue());
			}
		}
		return aliveTeamer;
	}

	// 获取我队中气血>0,且不包含sme动画的玩家列表(除自己)
	public Map<Byte, MmochatRole> getMyAliveTeamersExceptMeWithOutSmeState(
			int myPos, MmochatSmeType sme) {
		Map<Byte, MmochatRole> myTeam;
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			myTeam = leftTeam;
		} else {
			myTeam = rightTeam;
		}
		Map<Byte, MmochatRole> aliveTeamer = new ConcurrentHashMap<Byte, MmochatRole>();
		for (Entry<Byte, MmochatRole> entry : myTeam.entrySet()) {
			if (entry.getValue().getCurPkBlood() > 0 && entry.getKey() != myPos
					&& !entry.getValue().hasSme(sme)) {
				aliveTeamer.put(entry.getKey(), entry.getValue());
			}
		}
		return aliveTeamer;
	}

	public Map<Byte, MmochatRole> getTheOtherAliveTeamers(int myPos) {
		Map<Byte, MmochatRole> theOtherTeam;
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			theOtherTeam = rightTeam;
		} else {
			theOtherTeam = leftTeam;
		}
		Map<Byte, MmochatRole> aliveTeamer = new ConcurrentHashMap<Byte, MmochatRole>();
		for (Entry<Byte, MmochatRole> entry : theOtherTeam.entrySet()) {
			if (entry.getValue().getCurPkBlood() > 0) {
				aliveTeamer.put(entry.getKey(), entry.getValue());
			}
		}
		return aliveTeamer;
	}

	public Map<Byte, MmochatRole> getTheOtherTeam(int myPos) {
		if (myPos < MmochatConstant.rowNumInPk * 2) {
			return rightTeam;
		} else {
			return leftTeam;
		}
	}

	public Map<Byte, MmochatRole> getRoles() {
		return roles;
	}

	public void setRoles(Map<Byte, MmochatRole> roles) {
		this.roles = roles;
	}

	public Map<Byte, MmochatRole> getLeftTeam() {
		return leftTeam;
	}

	public void setLeftTeam(Map<Byte, MmochatRole> leftTeam) {
		this.leftTeam = leftTeam;
	}

	public Map<Byte, MmochatRole> getRightTeam() {
		return rightTeam;
	}

	public void setRightTeam(Map<Byte, MmochatRole> rightTeam) {
		this.rightTeam = rightTeam;
	}

	public Map<Byte, MmochatRole> getWinTeam() {
		return winTeam;
	}

	public void setWinTeam(Map<Byte, MmochatRole> winTeam) {
		this.winTeam = winTeam;
	}

	public Map<Byte, MmochatRole> getLoseTeam() {
		return loseTeam;
	}

	public void setLoseTeam(Map<Byte, MmochatRole> loseTeam) {
		this.loseTeam = loseTeam;
	}

	// 是否有一方全灭
	public boolean canBattleBeOver() {
		boolean leftLive = false, rightLive = false;
		for (MmochatRole role : leftTeam.values()) {
			if (role.getCurPkBlood() > 0) {
				leftLive = true;
				break;
			}
		}
		if (!leftLive) {
			// 左边输
			loseTeam = leftTeam;
			winTeam = rightTeam;
		}
		for (MmochatRole role : rightTeam.values()) {
			if (role.getCurPkBlood() > 0) {
				rightLive = true;
				break;
			}
		}
		if (!rightLive) {
			// 右边输
			winTeam = leftTeam;
			loseTeam = rightTeam;
		}
		if (winTeam != null && winTeam.size() > 0) {
			return true;
		}
		return false;
	}

	public MmochatBattleType getType() {
		return type;
	}

	public void setType(MmochatBattleType type) {
		this.type = type;
	}

	public void delRole(byte pos, boolean isKilled) {
		MmochatRole role = roles.get(pos);
		if (role instanceof MmochatPlayer) {
			MmochatPlayer player = (MmochatPlayer) role;
			player.leaveBattle();
		}
		roles.remove(pos);
		leftTeam.remove(pos);
		rightTeam.remove(pos);
		if (isKilled) {
			addKilledMonster(role);
		}
	}

	public void addRole(byte pos, MmochatRole role) {
		roles.put(pos, role);
		if (pos < 2 * MmochatConstant.rowNumInPk) {
			leftTeam.put(pos, role);
		} else {
			rightTeam.put(pos, role);
		}
	}

	public List<MmochatMonster> getKilledMonster() {
		return killedMonster;
	}

	public void setKilledMonster(List<MmochatMonster> killedMonster) {
		this.killedMonster = killedMonster;
	}

	public void addKilledMonster(MmochatRole role) {
		if (role == null) {
			return;
		}
		if (role instanceof MmochatPlayer || role instanceof MmochatPet) {
		} else if (role instanceof MmochatMonster) {
			MmochatMonster m = (MmochatMonster) role;
			killedMonster.add(m);
		}
	}

	public int getTmpNpcId() {
		return tmpNpcId;
	}

	public void setTmpNpcId(int tmpNpcId) {
		this.tmpNpcId = tmpNpcId;
	}

	public boolean isCanEscape() {
		return canEscape;
	}

	public void setCanEscape(boolean canEscape) {
		this.canEscape = canEscape;
	}

	public Map<Integer, MmochatPlayer> getAudiences() {
		return audiences;
	}

	public void setAudiences(Map<Integer, MmochatPlayer> audiences) {
		this.audiences = audiences;
	}

	public void addAudience(MmochatPlayer me) {
		if (state != MmochatBattleState.战斗结束) {
			audiences.put(me.getRoleId(), me);
			me.setWatchBattleId(battleId);
		}
	}

	public long getLastSystemMsgShowTick() {
		return lastSystemMsgShowTick;
	}

	public void setLastSystemMsgShowTick(long lastSystemMsgShowTick) {
		this.lastSystemMsgShowTick = lastSystemMsgShowTick;
	}

	public int getActionPlayNum() {
		return actionPlayNum;
	}

	public void setActionPlayNum(int actionPlayNum) {
		this.actionPlayNum = actionPlayNum;
	}

	public Map<Byte, Integer> getNewRoles_delRole() {
		return newRoles_delRole;
	}

	public void setNewRoles_delRole(Map<Byte, Integer> newRolesDelRole) {
		newRoles_delRole = newRolesDelRole;
	}

	public Map<Byte, MmochatRoundStateAddRole> getNewRoles_addRole() {
		return newRoles_addRole;
	}

	public void setNewRoles_addRole(
			Map<Byte, MmochatRoundStateAddRole> newRolesAddRole) {
		newRoles_addRole = newRolesAddRole;
	}

	public void addNewRoleState_delRole(byte pos, MmochatRole role) {
		newRoles_delRole.put(pos, role.getId());
	}

	public void addNewRoleState_addRole(byte pos, MmochatRole role) {
		MmochatRoundStateAddRole newRole = new MmochatRoundStateAddRole();
		newRole.setId(role.getId());
		newRole.setBmpType(role.getBmpType());
		newRole.setName(role.getName());
		newRole.setSmeValue(role.getSmeValue());
		newRole.setTotalBlood(role.getTotalBlood());
		newRole.setCurPkBlood(role.getCurPkBlood());
		newRole.setTotalSprit(role.getTotalSprit());
		newRole.setCurPkSprit(role.getCurPkSprit());
		newRole.setSkills(role.getSkills());
		if (role instanceof MmochatPlayer) {
			newRole.setRoleType(MmochatRoundStateAddRole.人物);
		} else {
			newRole.setRoleType(MmochatRoundStateAddRole.宠物);
		}
		newRoles_addRole.put(pos, newRole);
	}

	public void addNewRoleState_addRole(byte pos, MmochatRole role,
			MmochatRoleBmpType bmp) {
		MmochatRoundStateAddRole newRole = new MmochatRoundStateAddRole();
		newRole.setId(role.getId());
		newRole.setBmpType(bmp);
		newRole.setName(role.getName());
		newRole.setSmeValue(role.getSmeValue());
		newRole.setTotalBlood(role.getTotalBlood());
		newRole.setCurPkBlood(role.getCurPkBlood());
		newRole.setTotalSprit(role.getTotalSprit());
		newRole.setCurPkSprit(role.getCurPkSprit());
		newRole.setSkills(role.getSkills());
		if (role instanceof MmochatPlayer) {
			newRole.setRoleType(MmochatRoundStateAddRole.人物);
		} else {
			newRole.setRoleType(MmochatRoundStateAddRole.宠物);
		}
		newRoles_addRole.put(pos, newRole);
	}

	public long getBattleStartTime() {
		return battleStartTime;
	}

	public void setBattleStartTime(long battleStartTime) {
		this.battleStartTime = battleStartTime;
	}

}
